-/***************************************************************************/\r
-/* */\r
-/* pshalgo.c */\r
-/* */\r
-/* PostScript hinting algorithm (body). */\r
-/* */\r
-/* Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007 by */\r
-/* David Turner, Robert Wilhelm, and Werner Lemberg. */\r
-/* */\r
-/* This file is part of the FreeType project, and may only be used */\r
-/* modified and distributed under the terms of the FreeType project */\r
-/* license, LICENSE.TXT. By continuing to use, modify, or distribute */\r
-/* this file you indicate that you have read the license and */\r
-/* understand and accept it fully. */\r
-/* */\r
-/***************************************************************************/\r
-\r
-\r
-#include <ft2build.h>\r
-#include FT_INTERNAL_OBJECTS_H\r
-#include FT_INTERNAL_DEBUG_H\r
-#include FT_INTERNAL_CALC_H\r
-#include "pshalgo.h"\r
-\r
-#include "pshnterr.h"\r
-\r
-\r
-#undef FT_COMPONENT\r
-#define FT_COMPONENT trace_pshalgo2\r
-\r
-\r
-#ifdef DEBUG_HINTER\r
- PSH_Hint_Table ps_debug_hint_table = 0;\r
- PSH_HintFunc ps_debug_hint_func = 0;\r
- PSH_Glyph ps_debug_glyph = 0;\r
-#endif\r
-\r
-\r
-#define COMPUTE_INFLEXS /* compute inflection points to optimize `S' */\r
- /* and similar glyphs */\r
-#define STRONGER /* slightly increase the contrast of smooth */\r
- /* hinting */\r
-\r
-\r
- /*************************************************************************/\r
- /*************************************************************************/\r
- /***** *****/\r
- /***** BASIC HINTS RECORDINGS *****/\r
- /***** *****/\r
- /*************************************************************************/\r
- /*************************************************************************/\r
-\r
- /* return true if two stem hints overlap */\r
- static FT_Int\r
- psh_hint_overlap( PSH_Hint hint1,\r
- PSH_Hint hint2 )\r
- {\r
- return hint1->org_pos + hint1->org_len >= hint2->org_pos &&\r
- hint2->org_pos + hint2->org_len >= hint1->org_pos;\r
- }\r
-\r
-\r
- /* destroy hints table */\r
- static void\r
- psh_hint_table_done( PSH_Hint_Table table,\r
- FT_Memory memory )\r
- {\r
- FT_FREE( table->zones );\r
- table->num_zones = 0;\r
- table->zone = 0;\r
-\r
- FT_FREE( table->sort );\r
- FT_FREE( table->hints );\r
- table->num_hints = 0;\r
- table->max_hints = 0;\r
- table->sort_global = 0;\r
- }\r
-\r
-\r
- /* deactivate all hints in a table */\r
- static void\r
- psh_hint_table_deactivate( PSH_Hint_Table table )\r
- {\r
- FT_UInt count = table->max_hints;\r
- PSH_Hint hint = table->hints;\r
-\r
-\r
- for ( ; count > 0; count--, hint++ )\r
- {\r
- psh_hint_deactivate( hint );\r
- hint->order = -1;\r
- }\r
- }\r
-\r
-\r
- /* internal function to record a new hint */\r
- static void\r
- psh_hint_table_record( PSH_Hint_Table table,\r
- FT_UInt idx )\r
- {\r
- PSH_Hint hint = table->hints + idx;\r
-\r
-\r
- if ( idx >= table->max_hints )\r
- {\r
- FT_ERROR(( "psh_hint_table_record: invalid hint index %d\n", idx ));\r
- return;\r
- }\r
-\r
- /* ignore active hints */\r
- if ( psh_hint_is_active( hint ) )\r
- return;\r
-\r
- psh_hint_activate( hint );\r
-\r
- /* now scan the current active hint set to check */\r
- /* whether `hint' overlaps with another hint */\r
- {\r
- PSH_Hint* sorted = table->sort_global;\r
- FT_UInt count = table->num_hints;\r
- PSH_Hint hint2;\r
-\r
-\r
- hint->parent = 0;\r
- for ( ; count > 0; count--, sorted++ )\r
- {\r
- hint2 = sorted[0];\r
-\r
- if ( psh_hint_overlap( hint, hint2 ) )\r
- {\r
- hint->parent = hint2;\r
- break;\r
- }\r
- }\r
- }\r
-\r
- if ( table->num_hints < table->max_hints )\r
- table->sort_global[table->num_hints++] = hint;\r
- else\r
- FT_ERROR(( "psh_hint_table_record: too many sorted hints! BUG!\n" ));\r
- }\r
-\r
-\r
- static void\r
- psh_hint_table_record_mask( PSH_Hint_Table table,\r
- PS_Mask hint_mask )\r
- {\r
- FT_Int mask = 0, val = 0;\r
- FT_Byte* cursor = hint_mask->bytes;\r
- FT_UInt idx, limit;\r
-\r
-\r
- limit = hint_mask->num_bits;\r
-\r
- for ( idx = 0; idx < limit; idx++ )\r
- {\r
- if ( mask == 0 )\r
- {\r
- val = *cursor++;\r
- mask = 0x80;\r
- }\r
-\r
- if ( val & mask )\r
- psh_hint_table_record( table, idx );\r
-\r
- mask >>= 1;\r
- }\r
- }\r
-\r
-\r
- /* create hints table */\r
- static FT_Error\r
- psh_hint_table_init( PSH_Hint_Table table,\r
- PS_Hint_Table hints,\r
- PS_Mask_Table hint_masks,\r
- PS_Mask_Table counter_masks,\r
- FT_Memory memory )\r
- {\r
- FT_UInt count;\r
- FT_Error error;\r
-\r
- FT_UNUSED( counter_masks );\r
-\r
-\r
- count = hints->num_hints;\r
-\r
- /* allocate our tables */\r
- if ( FT_NEW_ARRAY( table->sort, 2 * count ) ||\r
- FT_NEW_ARRAY( table->hints, count ) ||\r
- FT_NEW_ARRAY( table->zones, 2 * count + 1 ) )\r
- goto Exit;\r
-\r
- table->max_hints = count;\r
- table->sort_global = table->sort + count;\r
- table->num_hints = 0;\r
- table->num_zones = 0;\r
- table->zone = 0;\r
-\r
- /* initialize the `table->hints' array */\r
- {\r
- PSH_Hint write = table->hints;\r
- PS_Hint read = hints->hints;\r
-\r
-\r
- for ( ; count > 0; count--, write++, read++ )\r
- {\r
- write->org_pos = read->pos;\r
- write->org_len = read->len;\r
- write->flags = read->flags;\r
- }\r
- }\r
-\r
- /* we now need to determine the initial `parent' stems; first */\r
- /* activate the hints that are given by the initial hint masks */\r
- if ( hint_masks )\r
- {\r
- PS_Mask mask = hint_masks->masks;\r
-\r
-\r
- count = hint_masks->num_masks;\r
- table->hint_masks = hint_masks;\r
-\r
- for ( ; count > 0; count--, mask++ )\r
- psh_hint_table_record_mask( table, mask );\r
- }\r
-\r
- /* finally, do a linear parse in case some hints were left alone */\r
- if ( table->num_hints != table->max_hints )\r
- {\r
- FT_UInt idx;\r
-\r
-\r
- FT_ERROR(( "psh_hint_table_init: missing/incorrect hint masks!\n" ));\r
-\r
- count = table->max_hints;\r
- for ( idx = 0; idx < count; idx++ )\r
- psh_hint_table_record( table, idx );\r
- }\r
-\r
- Exit:\r
- return error;\r
- }\r
-\r
-\r
- static void\r
- psh_hint_table_activate_mask( PSH_Hint_Table table,\r
- PS_Mask hint_mask )\r
- {\r
- FT_Int mask = 0, val = 0;\r
- FT_Byte* cursor = hint_mask->bytes;\r
- FT_UInt idx, limit, count;\r
-\r
-\r
- limit = hint_mask->num_bits;\r
- count = 0;\r
-\r
- psh_hint_table_deactivate( table );\r
-\r
- for ( idx = 0; idx < limit; idx++ )\r
- {\r
- if ( mask == 0 )\r
- {\r
- val = *cursor++;\r
- mask = 0x80;\r
- }\r
-\r
- if ( val & mask )\r
- {\r
- PSH_Hint hint = &table->hints[idx];\r
-\r
-\r
- if ( !psh_hint_is_active( hint ) )\r
- {\r
- FT_UInt count2;\r
-\r
-#if 0\r
- PSH_Hint* sort = table->sort;\r
- PSH_Hint hint2;\r
-\r
-\r
- for ( count2 = count; count2 > 0; count2--, sort++ )\r
- {\r
- hint2 = sort[0];\r
- if ( psh_hint_overlap( hint, hint2 ) )\r
- FT_ERROR(( "psh_hint_table_activate_mask:"\r
- " found overlapping hints\n" ))\r
- }\r
-#else\r
- count2 = 0;\r
-#endif\r
-\r
- if ( count2 == 0 )\r
- {\r
- psh_hint_activate( hint );\r
- if ( count < table->max_hints )\r
- table->sort[count++] = hint;\r
- else\r
- FT_ERROR(( "psh_hint_tableactivate_mask:"\r
- " too many active hints\n" ));\r
- }\r
- }\r
- }\r
-\r
- mask >>= 1;\r
- }\r
- table->num_hints = count;\r
-\r
- /* now, sort the hints; they are guaranteed to not overlap */\r
- /* so we can compare their "org_pos" field directly */\r
- {\r
- FT_Int i1, i2;\r
- PSH_Hint hint1, hint2;\r
- PSH_Hint* sort = table->sort;\r
-\r
-\r
- /* a simple bubble sort will do, since in 99% of cases, the hints */\r
- /* will be already sorted -- and the sort will be linear */\r
- for ( i1 = 1; i1 < (FT_Int)count; i1++ )\r
- {\r
- hint1 = sort[i1];\r
- for ( i2 = i1 - 1; i2 >= 0; i2-- )\r
- {\r
- hint2 = sort[i2];\r
-\r
- if ( hint2->org_pos < hint1->org_pos )\r
- break;\r
-\r
- sort[i2 + 1] = hint2;\r
- sort[i2] = hint1;\r
- }\r
- }\r
- }\r
- }\r
-\r
-\r
- /*************************************************************************/\r
- /*************************************************************************/\r
- /***** *****/\r
- /***** HINTS GRID-FITTING AND OPTIMIZATION *****/\r
- /***** *****/\r
- /*************************************************************************/\r
- /*************************************************************************/\r
-\r
-#if 1\r
- static FT_Pos\r
- psh_dimension_quantize_len( PSH_Dimension dim,\r
- FT_Pos len,\r
- FT_Bool do_snapping )\r
- {\r
- if ( len <= 64 )\r
- len = 64;\r
- else\r
- {\r
- FT_Pos delta = len - dim->stdw.widths[0].cur;\r
-\r
-\r
- if ( delta < 0 )\r
- delta = -delta;\r
-\r
- if ( delta < 40 )\r
- {\r
- len = dim->stdw.widths[0].cur;\r
- if ( len < 48 )\r
- len = 48;\r
- }\r
-\r
- if ( len < 3 * 64 )\r
- {\r
- delta = ( len & 63 );\r
- len &= -64;\r
-\r
- if ( delta < 10 )\r
- len += delta;\r
-\r
- else if ( delta < 32 )\r
- len += 10;\r
-\r
- else if ( delta < 54 )\r
- len += 54;\r
-\r
- else\r
- len += delta;\r
- }\r
- else\r
- len = FT_PIX_ROUND( len );\r
- }\r
-\r
- if ( do_snapping )\r
- len = FT_PIX_ROUND( len );\r
-\r
- return len;\r
- }\r
-#endif /* 0 */\r
-\r
-\r
-#ifdef DEBUG_HINTER\r
-\r
- static void\r
- ps_simple_scale( PSH_Hint_Table table,\r
- FT_Fixed scale,\r
- FT_Fixed delta,\r
- FT_Int dimension )\r
- {\r
- PSH_Hint hint;\r
- FT_UInt count;\r
-\r
-\r
- for ( count = 0; count < table->max_hints; count++ )\r
- {\r
- hint = table->hints + count;\r
-\r
- hint->cur_pos = FT_MulFix( hint->org_pos, scale ) + delta;\r
- hint->cur_len = FT_MulFix( hint->org_len, scale );\r
-\r
- if ( ps_debug_hint_func )\r
- ps_debug_hint_func( hint, dimension );\r
- }\r
- }\r
-\r
-#endif /* DEBUG_HINTER */\r
-\r
-\r
- static FT_Fixed\r
- psh_hint_snap_stem_side_delta( FT_Fixed pos,\r
- FT_Fixed len )\r
- {\r
- FT_Fixed delta1 = FT_PIX_ROUND( pos ) - pos;\r
- FT_Fixed delta2 = FT_PIX_ROUND( pos + len ) - pos - len;\r
-\r
-\r
- if ( FT_ABS( delta1 ) <= FT_ABS( delta2 ) )\r
- return delta1;\r
- else\r
- return delta2;\r
- }\r
-\r
-\r
- static void\r
- psh_hint_align( PSH_Hint hint,\r
- PSH_Globals globals,\r
- FT_Int dimension,\r
- PSH_Glyph glyph )\r
- {\r
- PSH_Dimension dim = &globals->dimension[dimension];\r
- FT_Fixed scale = dim->scale_mult;\r
- FT_Fixed delta = dim->scale_delta;\r
-\r
-\r
- if ( !psh_hint_is_fitted( hint ) )\r
- {\r
- FT_Pos pos = FT_MulFix( hint->org_pos, scale ) + delta;\r
- FT_Pos len = FT_MulFix( hint->org_len, scale );\r
-\r
- FT_Int do_snapping;\r
- FT_Pos fit_len;\r
- PSH_AlignmentRec align;\r
-\r
-\r
- /* ignore stem alignments when requested through the hint flags */\r
- if ( ( dimension == 0 && !glyph->do_horz_hints ) ||\r
- ( dimension == 1 && !glyph->do_vert_hints ) )\r
- {\r
- hint->cur_pos = pos;\r
- hint->cur_len = len;\r
-\r
- psh_hint_set_fitted( hint );\r
- return;\r
- }\r
-\r
- /* perform stem snapping when requested - this is necessary\r
- * for monochrome and LCD hinting modes only\r
- */\r
- do_snapping = ( dimension == 0 && glyph->do_horz_snapping ) ||\r
- ( dimension == 1 && glyph->do_vert_snapping );\r
-\r
- hint->cur_len = fit_len = len;\r
-\r
- /* check blue zones for horizontal stems */\r
- align.align = PSH_BLUE_ALIGN_NONE;\r
- align.align_bot = align.align_top = 0;\r
-\r
- if ( dimension == 1 )\r
- psh_blues_snap_stem( &globals->blues,\r
- hint->org_pos + hint->org_len,\r
- hint->org_pos,\r
- &align );\r
-\r
- switch ( align.align )\r
- {\r
- case PSH_BLUE_ALIGN_TOP:\r
- /* the top of the stem is aligned against a blue zone */\r
- hint->cur_pos = align.align_top - fit_len;\r
- break;\r
-\r
- case PSH_BLUE_ALIGN_BOT:\r
- /* the bottom of the stem is aligned against a blue zone */\r
- hint->cur_pos = align.align_bot;\r
- break;\r
-\r
- case PSH_BLUE_ALIGN_TOP | PSH_BLUE_ALIGN_BOT:\r
- /* both edges of the stem are aligned against blue zones */\r
- hint->cur_pos = align.align_bot;\r
- hint->cur_len = align.align_top - align.align_bot;\r
- break;\r
-\r
- default:\r
- {\r
- PSH_Hint parent = hint->parent;\r
-\r
-\r
- if ( parent )\r
- {\r
- FT_Pos par_org_center, par_cur_center;\r
- FT_Pos cur_org_center, cur_delta;\r
-\r
-\r
- /* ensure that parent is already fitted */\r
- if ( !psh_hint_is_fitted( parent ) )\r
- psh_hint_align( parent, globals, dimension, glyph );\r
-\r
- /* keep original relation between hints, this is, use the */\r
- /* scaled distance between the centers of the hints to */\r
- /* compute the new position */\r
- par_org_center = parent->org_pos + ( parent->org_len >> 1 );\r
- par_cur_center = parent->cur_pos + ( parent->cur_len >> 1 );\r
- cur_org_center = hint->org_pos + ( hint->org_len >> 1 );\r
-\r
- cur_delta = FT_MulFix( cur_org_center - par_org_center, scale );\r
- pos = par_cur_center + cur_delta - ( len >> 1 );\r
- }\r
-\r
- hint->cur_pos = pos;\r
- hint->cur_len = fit_len;\r
-\r
- /* Stem adjustment tries to snap stem widths to standard\r
- * ones. This is important to prevent unpleasant rounding\r
- * artefacts.\r
- */\r
- if ( glyph->do_stem_adjust )\r
- {\r
- if ( len <= 64 )\r
- {\r
- /* the stem is less than one pixel; we will center it\r
- * around the nearest pixel center\r
- */\r
- if ( len >= 32 )\r
- {\r
- /* This is a special case where we also widen the stem\r
- * and align it to the pixel grid.\r
- *\r
- * stem_center = pos + (len/2)\r
- * nearest_pixel_center = FT_ROUND(stem_center-32)+32\r
- * new_pos = nearest_pixel_center-32\r
- * = FT_ROUND(stem_center-32)\r
- * = FT_FLOOR(stem_center-32+32)\r
- * = FT_FLOOR(stem_center)\r
- * new_len = 64\r
- */\r
- pos = FT_PIX_FLOOR( pos + ( len >> 1 ) );\r
- len = 64;\r
- }\r
- else if ( len > 0 )\r
- {\r
- /* This is a very small stem; we simply align it to the\r
- * pixel grid, trying to find the minimal displacement.\r
- *\r
- * left = pos\r
- * right = pos + len\r
- * left_nearest_edge = ROUND(pos)\r
- * right_nearest_edge = ROUND(right)\r
- *\r
- * if ( ABS(left_nearest_edge - left) <=\r
- * ABS(right_nearest_edge - right) )\r
- * new_pos = left\r
- * else\r
- * new_pos = right\r
- */\r
- FT_Pos left_nearest = FT_PIX_ROUND( pos );\r
- FT_Pos right_nearest = FT_PIX_ROUND( pos + len );\r
- FT_Pos left_disp = left_nearest - pos;\r
- FT_Pos right_disp = right_nearest - ( pos + len );\r
-\r
-\r
- if ( left_disp < 0 )\r
- left_disp = -left_disp;\r
- if ( right_disp < 0 )\r
- right_disp = -right_disp;\r
- if ( left_disp <= right_disp )\r
- pos = left_nearest;\r
- else\r
- pos = right_nearest;\r
- }\r
- else\r
- {\r
- /* this is a ghost stem; we simply round it */\r
- pos = FT_PIX_ROUND( pos );\r
- }\r
- }\r
- else\r
- {\r
- len = psh_dimension_quantize_len( dim, len, 0 );\r
- }\r
- }\r
-\r
- /* now that we have a good hinted stem width, try to position */\r
- /* the stem along a pixel grid integer coordinate */\r
- hint->cur_pos = pos + psh_hint_snap_stem_side_delta( pos, len );\r
- hint->cur_len = len;\r
- }\r
- }\r
-\r
- if ( do_snapping )\r
- {\r
- pos = hint->cur_pos;\r
- len = hint->cur_len;\r
-\r
- if ( len < 64 )\r
- len = 64;\r
- else\r
- len = FT_PIX_ROUND( len );\r
-\r
- switch ( align.align )\r
- {\r
- case PSH_BLUE_ALIGN_TOP:\r
- hint->cur_pos = align.align_top - len;\r
- hint->cur_len = len;\r
- break;\r
-\r
- case PSH_BLUE_ALIGN_BOT:\r
- hint->cur_len = len;\r
- break;\r
-\r
- case PSH_BLUE_ALIGN_BOT | PSH_BLUE_ALIGN_TOP:\r
- /* don't touch */\r
- break;\r
-\r
-\r
- default:\r
- hint->cur_len = len;\r
- if ( len & 64 )\r
- pos = FT_PIX_FLOOR( pos + ( len >> 1 ) ) + 32;\r
- else\r
- pos = FT_PIX_ROUND( pos + ( len >> 1 ) );\r
-\r
- hint->cur_pos = pos - ( len >> 1 );\r
- hint->cur_len = len;\r
- }\r
- }\r
-\r
- psh_hint_set_fitted( hint );\r
-\r
-#ifdef DEBUG_HINTER\r
- if ( ps_debug_hint_func )\r
- ps_debug_hint_func( hint, dimension );\r
-#endif\r
- }\r
- }\r
-\r
-\r
-#if 0 /* not used for now, experimental */\r
-\r
- /*\r
- * A variant to perform "light" hinting (i.e. FT_RENDER_MODE_LIGHT)\r
- * of stems\r
- */\r
- static void\r
- psh_hint_align_light( PSH_Hint hint,\r
- PSH_Globals globals,\r
- FT_Int dimension,\r
- PSH_Glyph glyph )\r
- {\r
- PSH_Dimension dim = &globals->dimension[dimension];\r
- FT_Fixed scale = dim->scale_mult;\r
- FT_Fixed delta = dim->scale_delta;\r
-\r
-\r
- if ( !psh_hint_is_fitted( hint ) )\r
- {\r
- FT_Pos pos = FT_MulFix( hint->org_pos, scale ) + delta;\r
- FT_Pos len = FT_MulFix( hint->org_len, scale );\r
-\r
- FT_Pos fit_len;\r
-\r
- PSH_AlignmentRec align;\r
-\r
-\r
- /* ignore stem alignments when requested through the hint flags */\r
- if ( ( dimension == 0 && !glyph->do_horz_hints ) ||\r
- ( dimension == 1 && !glyph->do_vert_hints ) )\r
- {\r
- hint->cur_pos = pos;\r
- hint->cur_len = len;\r
-\r
- psh_hint_set_fitted( hint );\r
- return;\r
- }\r
-\r
- fit_len = len;\r
-\r
- hint->cur_len = fit_len;\r
-\r
- /* check blue zones for horizontal stems */\r
- align.align = PSH_BLUE_ALIGN_NONE;\r
- align.align_bot = align.align_top = 0;\r
-\r
- if ( dimension == 1 )\r
- psh_blues_snap_stem( &globals->blues,\r
- hint->org_pos + hint->org_len,\r
- hint->org_pos,\r
- &align );\r
-\r
- switch ( align.align )\r
- {\r
- case PSH_BLUE_ALIGN_TOP:\r
- /* the top of the stem is aligned against a blue zone */\r
- hint->cur_pos = align.align_top - fit_len;\r
- break;\r
-\r
- case PSH_BLUE_ALIGN_BOT:\r
- /* the bottom of the stem is aligned against a blue zone */\r
- hint->cur_pos = align.align_bot;\r
- break;\r
-\r
- case PSH_BLUE_ALIGN_TOP | PSH_BLUE_ALIGN_BOT:\r
- /* both edges of the stem are aligned against blue zones */\r
- hint->cur_pos = align.align_bot;\r
- hint->cur_len = align.align_top - align.align_bot;\r
- break;\r
-\r
- default:\r
- {\r
- PSH_Hint parent = hint->parent;\r
-\r
-\r
- if ( parent )\r
- {\r
- FT_Pos par_org_center, par_cur_center;\r
- FT_Pos cur_org_center, cur_delta;\r
-\r
-\r
- /* ensure that parent is already fitted */\r
- if ( !psh_hint_is_fitted( parent ) )\r
- psh_hint_align_light( parent, globals, dimension, glyph );\r
-\r
- par_org_center = parent->org_pos + ( parent->org_len / 2 );\r
- par_cur_center = parent->cur_pos + ( parent->cur_len / 2 );\r
- cur_org_center = hint->org_pos + ( hint->org_len / 2 );\r
-\r
- cur_delta = FT_MulFix( cur_org_center - par_org_center, scale );\r
- pos = par_cur_center + cur_delta - ( len >> 1 );\r
- }\r
-\r
- /* Stems less than one pixel wide are easy -- we want to\r
- * make them as dark as possible, so they must fall within\r
- * one pixel. If the stem is split between two pixels\r
- * then snap the edge that is nearer to the pixel boundary\r
- * to the pixel boundary.\r
- */\r
- if ( len <= 64 )\r
- {\r
- if ( ( pos + len + 63 ) / 64 != pos / 64 + 1 )\r
- pos += psh_hint_snap_stem_side_delta ( pos, len );\r
- }\r
-\r
- /* Position stems other to minimize the amount of mid-grays.\r
- * There are, in general, two positions that do this,\r
- * illustrated as A) and B) below.\r
- *\r
- * + + + +\r
- *\r
- * A) |--------------------------------|\r
- * B) |--------------------------------|\r
- * C) |--------------------------------|\r
- *\r
- * Position A) (split the excess stem equally) should be better\r
- * for stems of width N + f where f < 0.5.\r
- *\r
- * Position B) (split the deficiency equally) should be better\r
- * for stems of width N + f where f > 0.5.\r
- *\r
- * It turns out though that minimizing the total number of lit\r
- * pixels is also important, so position C), with one edge\r
- * aligned with a pixel boundary is actually preferable\r
- * to A). There are also more possibile positions for C) than\r
- * for A) or B), so it involves less distortion of the overall\r
- * character shape.\r
- */\r
- else /* len > 64 */\r
- {\r
- FT_Fixed frac_len = len & 63;\r
- FT_Fixed center = pos + ( len >> 1 );\r
- FT_Fixed delta_a, delta_b;\r
-\r
-\r
- if ( ( len / 64 ) & 1 )\r
- {\r
- delta_a = FT_PIX_FLOOR( center ) + 32 - center;\r
- delta_b = FT_PIX_ROUND( center ) - center;\r
- }\r
- else\r
- {\r
- delta_a = FT_PIX_ROUND( center ) - center;\r
- delta_b = FT_PIX_FLOOR( center ) + 32 - center;\r
- }\r
-\r
- /* We choose between B) and C) above based on the amount\r
- * of fractinal stem width; for small amounts, choose\r
- * C) always, for large amounts, B) always, and inbetween,\r
- * pick whichever one involves less stem movement.\r
- */\r
- if ( frac_len < 32 )\r
- {\r
- pos += psh_hint_snap_stem_side_delta ( pos, len );\r
- }\r
- else if ( frac_len < 48 )\r
- {\r
- FT_Fixed side_delta = psh_hint_snap_stem_side_delta ( pos,\r
- len );\r
-\r
- if ( FT_ABS( side_delta ) < FT_ABS( delta_b ) )\r
- pos += side_delta;\r
- else\r
- pos += delta_b;\r
- }\r
- else\r
- {\r
- pos += delta_b;\r
- }\r
- }\r
-\r
- hint->cur_pos = pos;\r
- }\r
- } /* switch */\r
-\r
- psh_hint_set_fitted( hint );\r
-\r
-#ifdef DEBUG_HINTER\r
- if ( ps_debug_hint_func )\r
- ps_debug_hint_func( hint, dimension );\r
-#endif\r
- }\r
- }\r
-\r
-#endif /* 0 */\r
-\r
-\r
- static void\r
- psh_hint_table_align_hints( PSH_Hint_Table table,\r
- PSH_Globals globals,\r
- FT_Int dimension,\r
- PSH_Glyph glyph )\r
- {\r
- PSH_Hint hint;\r
- FT_UInt count;\r
-\r
-#ifdef DEBUG_HINTER\r
-\r
- PSH_Dimension dim = &globals->dimension[dimension];\r
- FT_Fixed scale = dim->scale_mult;\r
- FT_Fixed delta = dim->scale_delta;\r
-\r
-\r
- if ( ps_debug_no_vert_hints && dimension == 0 )\r
- {\r
- ps_simple_scale( table, scale, delta, dimension );\r
- return;\r
- }\r
-\r
- if ( ps_debug_no_horz_hints && dimension == 1 )\r
- {\r
- ps_simple_scale( table, scale, delta, dimension );\r
- return;\r
- }\r
-\r
-#endif /* DEBUG_HINTER*/\r
-\r
- hint = table->hints;\r
- count = table->max_hints;\r
-\r
- for ( ; count > 0; count--, hint++ )\r
- psh_hint_align( hint, globals, dimension, glyph );\r
- }\r
-\r
-\r
- /*************************************************************************/\r
- /*************************************************************************/\r
- /***** *****/\r
- /***** POINTS INTERPOLATION ROUTINES *****/\r
- /***** *****/\r
- /*************************************************************************/\r
- /*************************************************************************/\r
-\r
-#define PSH_ZONE_MIN -3200000L\r
-#define PSH_ZONE_MAX +3200000L\r
-\r
-#define xxDEBUG_ZONES\r
-\r
-\r
-#ifdef DEBUG_ZONES\r
-\r
-#include <stdio.h>\r
-\r
- static void\r
- psh_print_zone( PSH_Zone zone )\r
- {\r
- printf( "zone [scale,delta,min,max] = [%.3f,%.3f,%d,%d]\n",\r
- zone->scale / 65536.0,\r
- zone->delta / 64.0,\r
- zone->min,\r
- zone->max );\r
- }\r
-\r
-#else\r
-\r
-#define psh_print_zone( x ) do { } while ( 0 )\r
-\r
-#endif /* DEBUG_ZONES */\r
-\r
-\r
- /*************************************************************************/\r
- /*************************************************************************/\r
- /***** *****/\r
- /***** HINTER GLYPH MANAGEMENT *****/\r
- /***** *****/\r
- /*************************************************************************/\r
- /*************************************************************************/\r
-\r
-#if 1\r
-\r
-#define psh_corner_is_flat ft_corner_is_flat\r
-#define psh_corner_orientation ft_corner_orientation\r
-\r
-#else\r
-\r
- FT_LOCAL_DEF( FT_Int )\r
- psh_corner_is_flat( FT_Pos x_in,\r
- FT_Pos y_in,\r
- FT_Pos x_out,\r
- FT_Pos y_out )\r
- {\r
- FT_Pos ax = x_in;\r
- FT_Pos ay = y_in;\r
-\r
- FT_Pos d_in, d_out, d_corner;\r
-\r
-\r
- if ( ax < 0 )\r
- ax = -ax;\r
- if ( ay < 0 )\r
- ay = -ay;\r
- d_in = ax + ay;\r
-\r
- ax = x_out;\r
- if ( ax < 0 )\r
- ax = -ax;\r
- ay = y_out;\r
- if ( ay < 0 )\r
- ay = -ay;\r
- d_out = ax + ay;\r
-\r
- ax = x_out + x_in;\r
- if ( ax < 0 )\r
- ax = -ax;\r
- ay = y_out + y_in;\r
- if ( ay < 0 )\r
- ay = -ay;\r
- d_corner = ax + ay;\r
-\r
- return ( d_in + d_out - d_corner ) < ( d_corner >> 4 );\r
- }\r
-\r
- static FT_Int\r
- psh_corner_orientation( FT_Pos in_x,\r
- FT_Pos in_y,\r
- FT_Pos out_x,\r
- FT_Pos out_y )\r
- {\r
- FT_Int result;\r
-\r
-\r
- /* deal with the trivial cases quickly */\r
- if ( in_y == 0 )\r
- {\r
- if ( in_x >= 0 )\r
- result = out_y;\r
- else\r
- result = -out_y;\r
- }\r
- else if ( in_x == 0 )\r
- {\r
- if ( in_y >= 0 )\r
- result = -out_x;\r
- else\r
- result = out_x;\r
- }\r
- else if ( out_y == 0 )\r
- {\r
- if ( out_x >= 0 )\r
- result = in_y;\r
- else\r
- result = -in_y;\r
- }\r
- else if ( out_x == 0 )\r
- {\r
- if ( out_y >= 0 )\r
- result = -in_x;\r
- else\r
- result = in_x;\r
- }\r
- else /* general case */\r
- {\r
- long long delta = (long long)in_x * out_y - (long long)in_y * out_x;\r
-\r
- if ( delta == 0 )\r
- result = 0;\r
- else\r
- result = 1 - 2 * ( delta < 0 );\r
- }\r
-\r
- return result;\r
- }\r
-\r
-#endif /* !1 */\r
-\r
-\r
-#ifdef COMPUTE_INFLEXS\r
-\r
- /* compute all inflex points in a given glyph */\r
- static void\r
- psh_glyph_compute_inflections( PSH_Glyph glyph )\r
- {\r
- FT_UInt n;\r
-\r
-\r
- for ( n = 0; n < glyph->num_contours; n++ )\r
- {\r
- PSH_Point first, start, end, before, after;\r
- FT_Pos in_x, in_y, out_x, out_y;\r
- FT_Int orient_prev, orient_cur;\r
- FT_Int finished = 0;\r
-\r
-\r
- /* we need at least 4 points to create an inflection point */\r
- if ( glyph->contours[n].count < 4 )\r
- continue;\r
-\r
- /* compute first segment in contour */\r
- first = glyph->contours[n].start;\r
-\r
- start = end = first;\r
- do\r
- {\r
- end = end->next;\r
- if ( end == first )\r
- goto Skip;\r
-\r
- in_x = end->org_u - start->org_u;\r
- in_y = end->org_v - start->org_v;\r
-\r
- } while ( in_x == 0 && in_y == 0 );\r
-\r
- /* extend the segment start whenever possible */\r
- before = start;\r
- do\r
- {\r
- do\r
- {\r
- start = before;\r
- before = before->prev;\r
- if ( before == first )\r
- goto Skip;\r
-\r
- out_x = start->org_u - before->org_u;\r
- out_y = start->org_v - before->org_v;\r
-\r
- } while ( out_x == 0 && out_y == 0 );\r
-\r
- orient_prev = psh_corner_orientation( in_x, in_y, out_x, out_y );\r
-\r
- } while ( orient_prev == 0 );\r
-\r
- first = start;\r
- in_x = out_x;\r
- in_y = out_y;\r
-\r
- /* now, process all segments in the contour */\r
- do\r
- {\r
- /* first, extend current segment's end whenever possible */\r
- after = end;\r
- do\r
- {\r
- do\r
- {\r
- end = after;\r
- after = after->next;\r
- if ( after == first )\r
- finished = 1;\r
-\r
- out_x = after->org_u - end->org_u;\r
- out_y = after->org_v - end->org_v;\r
-\r
- } while ( out_x == 0 && out_y == 0 );\r
-\r
- orient_cur = psh_corner_orientation( in_x, in_y, out_x, out_y );\r
-\r
- } while ( orient_cur == 0 );\r
-\r
- if ( ( orient_cur ^ orient_prev ) < 0 )\r
- {\r
- do\r
- {\r
- psh_point_set_inflex( start );\r
- start = start->next;\r
- }\r
- while ( start != end );\r
-\r
- psh_point_set_inflex( start );\r
- }\r
-\r
- start = end;\r
- end = after;\r
- orient_prev = orient_cur;\r
- in_x = out_x;\r
- in_y = out_y;\r
-\r
- } while ( !finished );\r
-\r
- Skip:\r
- ;\r
- }\r
- }\r
-\r
-#endif /* COMPUTE_INFLEXS */\r
-\r
-\r
- static void\r
- psh_glyph_done( PSH_Glyph glyph )\r
- {\r
- FT_Memory memory = glyph->memory;\r
-\r
-\r
- psh_hint_table_done( &glyph->hint_tables[1], memory );\r
- psh_hint_table_done( &glyph->hint_tables[0], memory );\r
-\r
- FT_FREE( glyph->points );\r
- FT_FREE( glyph->contours );\r
-\r
- glyph->num_points = 0;\r
- glyph->num_contours = 0;\r
-\r
- glyph->memory = 0;\r
- }\r
-\r
-\r
- static int\r
- psh_compute_dir( FT_Pos dx,\r
- FT_Pos dy )\r
- {\r
- FT_Pos ax, ay;\r
- int result = PSH_DIR_NONE;\r
-\r
-\r
- ax = ( dx >= 0 ) ? dx : -dx;\r
- ay = ( dy >= 0 ) ? dy : -dy;\r
-\r
- if ( ay * 12 < ax )\r
- {\r
- /* |dy| <<< |dx| means a near-horizontal segment */\r
- result = ( dx >= 0 ) ? PSH_DIR_RIGHT : PSH_DIR_LEFT;\r
- }\r
- else if ( ax * 12 < ay )\r
- {\r
- /* |dx| <<< |dy| means a near-vertical segment */\r
- result = ( dy >= 0 ) ? PSH_DIR_UP : PSH_DIR_DOWN;\r
- }\r
-\r
- return result;\r
- }\r
-\r
-\r
- /* load outline point coordinates into hinter glyph */\r
- static void\r
- psh_glyph_load_points( PSH_Glyph glyph,\r
- FT_Int dimension )\r
- {\r
- FT_Vector* vec = glyph->outline->points;\r
- PSH_Point point = glyph->points;\r
- FT_UInt count = glyph->num_points;\r
-\r
-\r
- for ( ; count > 0; count--, point++, vec++ )\r
- {\r
- point->flags2 = 0;\r
- point->hint = NULL;\r
- if ( dimension == 0 )\r
- {\r
- point->org_u = vec->x;\r
- point->org_v = vec->y;\r
- }\r
- else\r
- {\r
- point->org_u = vec->y;\r
- point->org_v = vec->x;\r
- }\r
-\r
-#ifdef DEBUG_HINTER\r
- point->org_x = vec->x;\r
- point->org_y = vec->y;\r
-#endif\r
-\r
- }\r
- }\r
-\r
-\r
- /* save hinted point coordinates back to outline */\r
- static void\r
- psh_glyph_save_points( PSH_Glyph glyph,\r
- FT_Int dimension )\r
- {\r
- FT_UInt n;\r
- PSH_Point point = glyph->points;\r
- FT_Vector* vec = glyph->outline->points;\r
- char* tags = glyph->outline->tags;\r
-\r
-\r
- for ( n = 0; n < glyph->num_points; n++ )\r
- {\r
- if ( dimension == 0 )\r
- vec[n].x = point->cur_u;\r
- else\r
- vec[n].y = point->cur_u;\r
-\r
- if ( psh_point_is_strong( point ) )\r
- tags[n] |= (char)( ( dimension == 0 ) ? 32 : 64 );\r
-\r
-#ifdef DEBUG_HINTER\r
-\r
- if ( dimension == 0 )\r
- {\r
- point->cur_x = point->cur_u;\r
- point->flags_x = point->flags2 | point->flags;\r
- }\r
- else\r
- {\r
- point->cur_y = point->cur_u;\r
- point->flags_y = point->flags2 | point->flags;\r
- }\r
-\r
-#endif\r
-\r
- point++;\r
- }\r
- }\r
-\r
-\r
- static FT_Error\r
- psh_glyph_init( PSH_Glyph glyph,\r
- FT_Outline* outline,\r
- PS_Hints ps_hints,\r
- PSH_Globals globals )\r
- {\r
- FT_Error error;\r
- FT_Memory memory;\r
-\r
-\r
- /* clear all fields */\r
- FT_MEM_ZERO( glyph, sizeof ( *glyph ) );\r
-\r
- memory = glyph->memory = globals->memory;\r
-\r
- /* allocate and setup points + contours arrays */\r
- if ( FT_NEW_ARRAY( glyph->points, outline->n_points ) ||\r
- FT_NEW_ARRAY( glyph->contours, outline->n_contours ) )\r
- goto Exit;\r
-\r
- glyph->num_points = outline->n_points;\r
- glyph->num_contours = outline->n_contours;\r
-\r
- {\r
- FT_UInt first = 0, next, n;\r
- PSH_Point points = glyph->points;\r
- PSH_Contour contour = glyph->contours;\r
-\r
-\r
- for ( n = 0; n < glyph->num_contours; n++ )\r
- {\r
- FT_Int count;\r
- PSH_Point point;\r
-\r
-\r
- next = outline->contours[n] + 1;\r
- count = next - first;\r
-\r
- contour->start = points + first;\r
- contour->count = (FT_UInt)count;\r
-\r
- if ( count > 0 )\r
- {\r
- point = points + first;\r
-\r
- point->prev = points + next - 1;\r
- point->contour = contour;\r
-\r
- for ( ; count > 1; count-- )\r
- {\r
- point[0].next = point + 1;\r
- point[1].prev = point;\r
- point++;\r
- point->contour = contour;\r
- }\r
- point->next = points + first;\r
- }\r
-\r
- contour++;\r
- first = next;\r
- }\r
- }\r
-\r
- {\r
- PSH_Point points = glyph->points;\r
- PSH_Point point = points;\r
- FT_Vector* vec = outline->points;\r
- FT_UInt n;\r
-\r
-\r
- for ( n = 0; n < glyph->num_points; n++, point++ )\r
- {\r
- FT_Int n_prev = (FT_Int)( point->prev - points );\r
- FT_Int n_next = (FT_Int)( point->next - points );\r
- FT_Pos dxi, dyi, dxo, dyo;\r
-\r
-\r
- if ( !( outline->tags[n] & FT_CURVE_TAG_ON ) )\r
- point->flags = PSH_POINT_OFF;\r
-\r
- dxi = vec[n].x - vec[n_prev].x;\r
- dyi = vec[n].y - vec[n_prev].y;\r
-\r
- point->dir_in = (FT_Char)psh_compute_dir( dxi, dyi );\r
-\r
- dxo = vec[n_next].x - vec[n].x;\r
- dyo = vec[n_next].y - vec[n].y;\r
-\r
- point->dir_out = (FT_Char)psh_compute_dir( dxo, dyo );\r
-\r
- /* detect smooth points */\r
- if ( point->flags & PSH_POINT_OFF )\r
- point->flags |= PSH_POINT_SMOOTH;\r
-\r
- else if ( point->dir_in == point->dir_out )\r
- {\r
- if ( point->dir_out != PSH_DIR_NONE ||\r
- psh_corner_is_flat( dxi, dyi, dxo, dyo ) )\r
- point->flags |= PSH_POINT_SMOOTH;\r
- }\r
- }\r
- }\r
-\r
- glyph->outline = outline;\r
- glyph->globals = globals;\r
-\r
-#ifdef COMPUTE_INFLEXS\r
- psh_glyph_load_points( glyph, 0 );\r
- psh_glyph_compute_inflections( glyph );\r
-#endif /* COMPUTE_INFLEXS */\r
-\r
- /* now deal with hints tables */\r
- error = psh_hint_table_init( &glyph->hint_tables [0],\r
- &ps_hints->dimension[0].hints,\r
- &ps_hints->dimension[0].masks,\r
- &ps_hints->dimension[0].counters,\r
- memory );\r
- if ( error )\r
- goto Exit;\r
-\r
- error = psh_hint_table_init( &glyph->hint_tables [1],\r
- &ps_hints->dimension[1].hints,\r
- &ps_hints->dimension[1].masks,\r
- &ps_hints->dimension[1].counters,\r
- memory );\r
- if ( error )\r
- goto Exit;\r
-\r
- Exit:\r
- return error;\r
- }\r
-\r
-\r
- /* compute all extrema in a glyph for a given dimension */\r
- static void\r
- psh_glyph_compute_extrema( PSH_Glyph glyph )\r
- {\r
- FT_UInt n;\r
-\r
-\r
- /* first of all, compute all local extrema */\r
- for ( n = 0; n < glyph->num_contours; n++ )\r
- {\r
- PSH_Point first = glyph->contours[n].start;\r
- PSH_Point point, before, after;\r
-\r
-\r
- if ( glyph->contours[n].count == 0 )\r
- continue;\r
-\r
- point = first;\r
- before = point;\r
- after = point;\r
-\r
- do\r
- {\r
- before = before->prev;\r
- if ( before == first )\r
- goto Skip;\r
-\r
- } while ( before->org_u == point->org_u );\r
-\r
- first = point = before->next;\r
-\r
- for (;;)\r
- {\r
- after = point;\r
- do\r
- {\r
- after = after->next;\r
- if ( after == first )\r
- goto Next;\r
-\r
- } while ( after->org_u == point->org_u );\r
-\r
- if ( before->org_u < point->org_u )\r
- {\r
- if ( after->org_u < point->org_u )\r
- {\r
- /* local maximum */\r
- goto Extremum;\r
- }\r
- }\r
- else /* before->org_u > point->org_u */\r
- {\r
- if ( after->org_u > point->org_u )\r
- {\r
- /* local minimum */\r
- Extremum:\r
- do\r
- {\r
- psh_point_set_extremum( point );\r
- point = point->next;\r
-\r
- } while ( point != after );\r
- }\r
- }\r
-\r
- before = after->prev;\r
- point = after;\r
-\r
- } /* for */\r
-\r
- Next:\r
- ;\r
- }\r
-\r
- /* for each extremum, determine its direction along the */\r
- /* orthogonal axis */\r
- for ( n = 0; n < glyph->num_points; n++ )\r
- {\r
- PSH_Point point, before, after;\r
-\r
-\r
- point = &glyph->points[n];\r
- before = point;\r
- after = point;\r
-\r
- if ( psh_point_is_extremum( point ) )\r
- {\r
- do\r
- {\r
- before = before->prev;\r
- if ( before == point )\r
- goto Skip;\r
-\r
- } while ( before->org_v == point->org_v );\r
-\r
- do\r
- {\r
- after = after->next;\r
- if ( after == point )\r
- goto Skip;\r
-\r
- } while ( after->org_v == point->org_v );\r
- }\r
-\r
- if ( before->org_v < point->org_v &&\r
- after->org_v > point->org_v )\r
- {\r
- psh_point_set_positive( point );\r
- }\r
- else if ( before->org_v > point->org_v &&\r
- after->org_v < point->org_v )\r
- {\r
- psh_point_set_negative( point );\r
- }\r
-\r
- Skip:\r
- ;\r
- }\r
- }\r
-\r
-\r
- /* major_dir is the direction for points on the bottom/left of the stem; */\r
- /* Points on the top/right of the stem will have a direction of */\r
- /* -major_dir. */\r
-\r
- static void\r
- psh_hint_table_find_strong_points( PSH_Hint_Table table,\r
- PSH_Point point,\r
- FT_UInt count,\r
- FT_Int threshold,\r
- FT_Int major_dir )\r
- {\r
- PSH_Hint* sort = table->sort;\r
- FT_UInt num_hints = table->num_hints;\r
-\r
-\r
- for ( ; count > 0; count--, point++ )\r
- {\r
- FT_Int point_dir = 0;\r
- FT_Pos org_u = point->org_u;\r
-\r
-\r
- if ( psh_point_is_strong( point ) )\r
- continue;\r
-\r
- if ( PSH_DIR_COMPARE( point->dir_in, major_dir ) )\r
- point_dir = point->dir_in;\r
-\r
- else if ( PSH_DIR_COMPARE( point->dir_out, major_dir ) )\r
- point_dir = point->dir_out;\r
-\r
- if ( point_dir )\r
- {\r
- if ( point_dir == major_dir )\r
- {\r
- FT_UInt nn;\r
-\r
-\r
- for ( nn = 0; nn < num_hints; nn++ )\r
- {\r
- PSH_Hint hint = sort[nn];\r
- FT_Pos d = org_u - hint->org_pos;\r
-\r
-\r
- if ( d < threshold && -d < threshold )\r
- {\r
- psh_point_set_strong( point );\r
- point->flags2 |= PSH_POINT_EDGE_MIN;\r
- point->hint = hint;\r
- break;\r
- }\r
- }\r
- }\r
- else if ( point_dir == -major_dir )\r
- {\r
- FT_UInt nn;\r
-\r
-\r
- for ( nn = 0; nn < num_hints; nn++ )\r
- {\r
- PSH_Hint hint = sort[nn];\r
- FT_Pos d = org_u - hint->org_pos - hint->org_len;\r
-\r
-\r
- if ( d < threshold && -d < threshold )\r
- {\r
- psh_point_set_strong( point );\r
- point->flags2 |= PSH_POINT_EDGE_MAX;\r
- point->hint = hint;\r
- break;\r
- }\r
- }\r
- }\r
- }\r
-\r
-#if 1\r
- else if ( psh_point_is_extremum( point ) )\r
- {\r
- /* treat extrema as special cases for stem edge alignment */\r
- FT_UInt nn, min_flag, max_flag;\r
-\r
-\r
- if ( major_dir == PSH_DIR_HORIZONTAL )\r
- {\r
- min_flag = PSH_POINT_POSITIVE;\r
- max_flag = PSH_POINT_NEGATIVE;\r
- }\r
- else\r
- {\r
- min_flag = PSH_POINT_NEGATIVE;\r
- max_flag = PSH_POINT_POSITIVE;\r
- }\r
-\r
- if ( point->flags2 & min_flag )\r
- {\r
- for ( nn = 0; nn < num_hints; nn++ )\r
- {\r
- PSH_Hint hint = sort[nn];\r
- FT_Pos d = org_u - hint->org_pos;\r
-\r
-\r
- if ( d < threshold && -d < threshold )\r
- {\r
- point->flags2 |= PSH_POINT_EDGE_MIN;\r
- point->hint = hint;\r
- psh_point_set_strong( point );\r
- break;\r
- }\r
- }\r
- }\r
- else if ( point->flags2 & max_flag )\r
- {\r
- for ( nn = 0; nn < num_hints; nn++ )\r
- {\r
- PSH_Hint hint = sort[nn];\r
- FT_Pos d = org_u - hint->org_pos - hint->org_len;\r
-\r
-\r
- if ( d < threshold && -d < threshold )\r
- {\r
- point->flags2 |= PSH_POINT_EDGE_MAX;\r
- point->hint = hint;\r
- psh_point_set_strong( point );\r
- break;\r
- }\r
- }\r
- }\r
-\r
- if ( point->hint == NULL )\r
- {\r
- for ( nn = 0; nn < num_hints; nn++ )\r
- {\r
- PSH_Hint hint = sort[nn];\r
-\r
-\r
- if ( org_u >= hint->org_pos &&\r
- org_u <= hint->org_pos + hint->org_len )\r
- {\r
- point->hint = hint;\r
- break;\r
- }\r
- }\r
- }\r
- }\r
-\r
-#endif /* 1 */\r
- }\r
- }\r
-\r
-\r
- /* the accepted shift for strong points in fractional pixels */\r
-#define PSH_STRONG_THRESHOLD 32\r
-\r
- /* the maximum shift value in font units */\r
-#define PSH_STRONG_THRESHOLD_MAXIMUM 30\r
-\r
-\r
- /* find strong points in a glyph */\r
- static void\r
- psh_glyph_find_strong_points( PSH_Glyph glyph,\r
- FT_Int dimension )\r
- {\r
- /* a point is `strong' if it is located on a stem edge and */\r
- /* has an `in' or `out' tangent parallel to the hint's direction */\r
-\r
- PSH_Hint_Table table = &glyph->hint_tables[dimension];\r
- PS_Mask mask = table->hint_masks->masks;\r
- FT_UInt num_masks = table->hint_masks->num_masks;\r
- FT_UInt first = 0;\r
- FT_Int major_dir = dimension == 0 ? PSH_DIR_VERTICAL\r
- : PSH_DIR_HORIZONTAL;\r
- PSH_Dimension dim = &glyph->globals->dimension[dimension];\r
- FT_Fixed scale = dim->scale_mult;\r
- FT_Int threshold;\r
-\r
-\r
- threshold = (FT_Int)FT_DivFix( PSH_STRONG_THRESHOLD, scale );\r
- if ( threshold > PSH_STRONG_THRESHOLD_MAXIMUM )\r
- threshold = PSH_STRONG_THRESHOLD_MAXIMUM;\r
-\r
- /* process secondary hints to `selected' points */\r
- if ( num_masks > 1 && glyph->num_points > 0 )\r
- {\r
- first = mask->end_point;\r
- mask++;\r
- for ( ; num_masks > 1; num_masks--, mask++ )\r
- {\r
- FT_UInt next;\r
- FT_Int count;\r
-\r
-\r
- next = mask->end_point;\r
- count = next - first;\r
- if ( count > 0 )\r
- {\r
- PSH_Point point = glyph->points + first;\r
-\r
-\r
- psh_hint_table_activate_mask( table, mask );\r
-\r
- psh_hint_table_find_strong_points( table, point, count,\r
- threshold, major_dir );\r
- }\r
- first = next;\r
- }\r
- }\r
-\r
- /* process primary hints for all points */\r
- if ( num_masks == 1 )\r
- {\r
- FT_UInt count = glyph->num_points;\r
- PSH_Point point = glyph->points;\r
-\r
-\r
- psh_hint_table_activate_mask( table, table->hint_masks->masks );\r
-\r
- psh_hint_table_find_strong_points( table, point, count,\r
- threshold, major_dir );\r
- }\r
-\r
- /* now, certain points may have been attached to a hint and */\r
- /* not marked as strong; update their flags then */\r
- {\r
- FT_UInt count = glyph->num_points;\r
- PSH_Point point = glyph->points;\r
-\r
-\r
- for ( ; count > 0; count--, point++ )\r
- if ( point->hint && !psh_point_is_strong( point ) )\r
- psh_point_set_strong( point );\r
- }\r
- }\r
-\r
-\r
- /* find points in a glyph which are in a blue zone and have `in' or */\r
- /* `out' tangents parallel to the horizontal axis */\r
- static void\r
- psh_glyph_find_blue_points( PSH_Blues blues,\r
- PSH_Glyph glyph )\r
- {\r
- PSH_Blue_Table table;\r
- PSH_Blue_Zone zone;\r
- FT_UInt glyph_count = glyph->num_points;\r
- FT_UInt blue_count;\r
- PSH_Point point = glyph->points;\r
-\r
-\r
- for ( ; glyph_count > 0; glyph_count--, point++ )\r
- {\r
- FT_Pos y;\r
-\r
-\r
- /* check tangents */\r
- if ( !PSH_DIR_COMPARE( point->dir_in, PSH_DIR_HORIZONTAL ) &&\r
- !PSH_DIR_COMPARE( point->dir_out, PSH_DIR_HORIZONTAL ) )\r
- continue;\r
-\r
- /* skip strong points */\r
- if ( psh_point_is_strong( point ) )\r
- continue;\r
-\r
- y = point->org_u;\r
-\r
- /* look up top zones */\r
- table = &blues->normal_top;\r
- blue_count = table->count;\r
- zone = table->zones;\r
-\r
- for ( ; blue_count > 0; blue_count--, zone++ )\r
- {\r
- FT_Pos delta = y - zone->org_bottom;\r
-\r
-\r
- if ( delta < -blues->blue_fuzz )\r
- break;\r
-\r
- if ( y <= zone->org_top + blues->blue_fuzz )\r
- if ( blues->no_overshoots || delta <= blues->blue_threshold )\r
- {\r
- point->cur_u = zone->cur_bottom;\r
- psh_point_set_strong( point );\r
- psh_point_set_fitted( point );\r
- }\r
- }\r
-\r
- /* look up bottom zones */\r
- table = &blues->normal_bottom;\r
- blue_count = table->count;\r
- zone = table->zones + blue_count - 1;\r
-\r
- for ( ; blue_count > 0; blue_count--, zone-- )\r
- {\r
- FT_Pos delta = zone->org_top - y;\r
-\r
-\r
- if ( delta < -blues->blue_fuzz )\r
- break;\r
-\r
- if ( y >= zone->org_bottom - blues->blue_fuzz )\r
- if ( blues->no_overshoots || delta < blues->blue_threshold )\r
- {\r
- point->cur_u = zone->cur_top;\r
- psh_point_set_strong( point );\r
- psh_point_set_fitted( point );\r
- }\r
- }\r
- }\r
- }\r
-\r
-\r
- /* interpolate strong points with the help of hinted coordinates */\r
- static void\r
- psh_glyph_interpolate_strong_points( PSH_Glyph glyph,\r
- FT_Int dimension )\r
- {\r
- PSH_Dimension dim = &glyph->globals->dimension[dimension];\r
- FT_Fixed scale = dim->scale_mult;\r
-\r
- FT_UInt count = glyph->num_points;\r
- PSH_Point point = glyph->points;\r
-\r
-\r
- for ( ; count > 0; count--, point++ )\r
- {\r
- PSH_Hint hint = point->hint;\r
-\r
-\r
- if ( hint )\r
- {\r
- FT_Pos delta;\r
-\r
-\r
- if ( psh_point_is_edge_min( point ) )\r
- point->cur_u = hint->cur_pos;\r
-\r
- else if ( psh_point_is_edge_max( point ) )\r
- point->cur_u = hint->cur_pos + hint->cur_len;\r
-\r
- else\r
- {\r
- delta = point->org_u - hint->org_pos;\r
-\r
- if ( delta <= 0 )\r
- point->cur_u = hint->cur_pos + FT_MulFix( delta, scale );\r
-\r
- else if ( delta >= hint->org_len )\r
- point->cur_u = hint->cur_pos + hint->cur_len +\r
- FT_MulFix( delta - hint->org_len, scale );\r
-\r
- else if ( hint->org_len > 0 )\r
- point->cur_u = hint->cur_pos +\r
- FT_MulDiv( delta, hint->cur_len,\r
- hint->org_len );\r
- else\r
- point->cur_u = hint->cur_pos;\r
- }\r
- psh_point_set_fitted( point );\r
- }\r
- }\r
- }\r
-\r
-\r
-#define PSH_MAX_STRONG_INTERNAL 16\r
-\r
- static void\r
- psh_glyph_interpolate_normal_points( PSH_Glyph glyph,\r
- FT_Int dimension )\r
- {\r
-\r
-#if 1\r
- /* first technique: a point is strong if it is a local extremum */\r
-\r
- PSH_Dimension dim = &glyph->globals->dimension[dimension];\r
- FT_Fixed scale = dim->scale_mult;\r
- FT_Memory memory = glyph->memory;\r
-\r
- PSH_Point* strongs = NULL;\r
- PSH_Point strongs_0[PSH_MAX_STRONG_INTERNAL];\r
- FT_UInt num_strongs = 0;\r
-\r
- PSH_Point points = glyph->points;\r
- PSH_Point points_end = points + glyph->num_points;\r
- PSH_Point point;\r
-\r
-\r
- /* first count the number of strong points */\r
- for ( point = points; point < points_end; point++ )\r
- {\r
- if ( psh_point_is_strong( point ) )\r
- num_strongs++;\r
- }\r
-\r
- if ( num_strongs == 0 ) /* nothing to do here */\r
- return;\r
-\r
- /* allocate an array to store a list of points, */\r
- /* stored in increasing org_u order */\r
- if ( num_strongs <= PSH_MAX_STRONG_INTERNAL )\r
- strongs = strongs_0;\r
- else\r
- {\r
- FT_Error error;\r
-\r
-\r
- if ( FT_NEW_ARRAY( strongs, num_strongs ) )\r
- return;\r
- }\r
-\r
- num_strongs = 0;\r
- for ( point = points; point < points_end; point++ )\r
- {\r
- PSH_Point* insert;\r
-\r
-\r
- if ( !psh_point_is_strong( point ) )\r
- continue;\r
-\r
- for ( insert = strongs + num_strongs; insert > strongs; insert-- )\r
- {\r
- if ( insert[-1]->org_u <= point->org_u )\r
- break;\r
-\r
- insert[0] = insert[-1];\r
- }\r
- insert[0] = point;\r
- num_strongs++;\r
- }\r
-\r
- /* now try to interpolate all normal points */\r
- for ( point = points; point < points_end; point++ )\r
- {\r
- if ( psh_point_is_strong( point ) )\r
- continue;\r
-\r
- /* sometimes, some local extrema are smooth points */\r
- if ( psh_point_is_smooth( point ) )\r
- {\r
- if ( point->dir_in == PSH_DIR_NONE ||\r
- point->dir_in != point->dir_out )\r
- continue;\r
-\r
- if ( !psh_point_is_extremum( point ) &&\r
- !psh_point_is_inflex( point ) )\r
- continue;\r
-\r
- point->flags &= ~PSH_POINT_SMOOTH;\r
- }\r
-\r
- /* find best enclosing point coordinates then interpolate */\r
- {\r
- PSH_Point before, after;\r
- FT_UInt nn;\r
-\r
-\r
- for ( nn = 0; nn < num_strongs; nn++ )\r
- if ( strongs[nn]->org_u > point->org_u )\r
- break;\r
-\r
- if ( nn == 0 ) /* point before the first strong point */\r
- {\r
- after = strongs[0];\r
-\r
- point->cur_u = after->cur_u +\r
- FT_MulFix( point->org_u - after->org_u,\r
- scale );\r
- }\r
- else\r
- {\r
- before = strongs[nn - 1];\r
-\r
- for ( nn = num_strongs; nn > 0; nn-- )\r
- if ( strongs[nn - 1]->org_u < point->org_u )\r
- break;\r
-\r
- if ( nn == num_strongs ) /* point is after last strong point */\r
- {\r
- before = strongs[nn - 1];\r
-\r
- point->cur_u = before->cur_u +\r
- FT_MulFix( point->org_u - before->org_u,\r
- scale );\r
- }\r
- else\r
- {\r
- FT_Pos u;\r
-\r
-\r
- after = strongs[nn];\r
-\r
- /* now interpolate point between before and after */\r
- u = point->org_u;\r
-\r
- if ( u == before->org_u )\r
- point->cur_u = before->cur_u;\r
-\r
- else if ( u == after->org_u )\r
- point->cur_u = after->cur_u;\r
-\r
- else\r
- point->cur_u = before->cur_u +\r
- FT_MulDiv( u - before->org_u,\r
- after->cur_u - before->cur_u,\r
- after->org_u - before->org_u );\r
- }\r
- }\r
- psh_point_set_fitted( point );\r
- }\r
- }\r
-\r
- if ( strongs != strongs_0 )\r
- FT_FREE( strongs );\r
-\r
-#endif /* 1 */\r
-\r
- }\r
-\r
-\r
- /* interpolate other points */\r
- static void\r
- psh_glyph_interpolate_other_points( PSH_Glyph glyph,\r
- FT_Int dimension )\r
- {\r
- PSH_Dimension dim = &glyph->globals->dimension[dimension];\r
- FT_Fixed scale = dim->scale_mult;\r
- FT_Fixed delta = dim->scale_delta;\r
- PSH_Contour contour = glyph->contours;\r
- FT_UInt num_contours = glyph->num_contours;\r
-\r
-\r
- for ( ; num_contours > 0; num_contours--, contour++ )\r
- {\r
- PSH_Point start = contour->start;\r
- PSH_Point first, next, point;\r
- FT_UInt fit_count;\r
-\r
-\r
- /* count the number of strong points in this contour */\r
- next = start + contour->count;\r
- fit_count = 0;\r
- first = 0;\r
-\r
- for ( point = start; point < next; point++ )\r
- if ( psh_point_is_fitted( point ) )\r
- {\r
- if ( !first )\r
- first = point;\r
-\r
- fit_count++;\r
- }\r
-\r
- /* if there are less than 2 fitted points in the contour, we */\r
- /* simply scale and eventually translate the contour points */\r
- if ( fit_count < 2 )\r
- {\r
- if ( fit_count == 1 )\r
- delta = first->cur_u - FT_MulFix( first->org_u, scale );\r
-\r
- for ( point = start; point < next; point++ )\r
- if ( point != first )\r
- point->cur_u = FT_MulFix( point->org_u, scale ) + delta;\r
-\r
- goto Next_Contour;\r
- }\r
-\r
- /* there are more than 2 strong points in this contour; we */\r
- /* need to interpolate weak points between them */\r
- start = first;\r
- do\r
- {\r
- point = first;\r
-\r
- /* skip consecutive fitted points */\r
- for (;;)\r
- {\r
- next = first->next;\r
- if ( next == start )\r
- goto Next_Contour;\r
-\r
- if ( !psh_point_is_fitted( next ) )\r
- break;\r
-\r
- first = next;\r
- }\r
-\r
- /* find next fitted point after unfitted one */\r
- for (;;)\r
- {\r
- next = next->next;\r
- if ( psh_point_is_fitted( next ) )\r
- break;\r
- }\r
-\r
- /* now interpolate between them */\r
- {\r
- FT_Pos org_a, org_ab, cur_a, cur_ab;\r
- FT_Pos org_c, org_ac, cur_c;\r
- FT_Fixed scale_ab;\r
-\r
-\r
- if ( first->org_u <= next->org_u )\r
- {\r
- org_a = first->org_u;\r
- cur_a = first->cur_u;\r
- org_ab = next->org_u - org_a;\r
- cur_ab = next->cur_u - cur_a;\r
- }\r
- else\r
- {\r
- org_a = next->org_u;\r
- cur_a = next->cur_u;\r
- org_ab = first->org_u - org_a;\r
- cur_ab = first->cur_u - cur_a;\r
- }\r
-\r
- scale_ab = 0x10000L;\r
- if ( org_ab > 0 )\r
- scale_ab = FT_DivFix( cur_ab, org_ab );\r
-\r
- point = first->next;\r
- do\r
- {\r
- org_c = point->org_u;\r
- org_ac = org_c - org_a;\r
-\r
- if ( org_ac <= 0 )\r
- {\r
- /* on the left of the interpolation zone */\r
- cur_c = cur_a + FT_MulFix( org_ac, scale );\r
- }\r
- else if ( org_ac >= org_ab )\r
- {\r
- /* on the right on the interpolation zone */\r
- cur_c = cur_a + cur_ab + FT_MulFix( org_ac - org_ab, scale );\r
- }\r
- else\r
- {\r
- /* within the interpolation zone */\r
- cur_c = cur_a + FT_MulFix( org_ac, scale_ab );\r
- }\r
-\r
- point->cur_u = cur_c;\r
-\r
- point = point->next;\r
-\r
- } while ( point != next );\r
- }\r
-\r
- /* keep going until all points in the contours have been processed */\r
- first = next;\r
-\r
- } while ( first != start );\r
-\r
- Next_Contour:\r
- ;\r
- }\r
- }\r
-\r
-\r
- /*************************************************************************/\r
- /*************************************************************************/\r
- /***** *****/\r
- /***** HIGH-LEVEL INTERFACE *****/\r
- /***** *****/\r
- /*************************************************************************/\r
- /*************************************************************************/\r
-\r
- FT_Error\r
- ps_hints_apply( PS_Hints ps_hints,\r
- FT_Outline* outline,\r
- PSH_Globals globals,\r
- FT_Render_Mode hint_mode )\r
- {\r
- PSH_GlyphRec glyphrec;\r
- PSH_Glyph glyph = &glyphrec;\r
- FT_Error error;\r
-#ifdef DEBUG_HINTER\r
- FT_Memory memory;\r
-#endif\r
- FT_Int dimension;\r
-\r
-\r
- /* something to do? */\r
- if ( outline->n_points == 0 || outline->n_contours == 0 )\r
- return PSH_Err_Ok;\r
-\r
-#ifdef DEBUG_HINTER\r
-\r
- memory = globals->memory;\r
-\r
- if ( ps_debug_glyph )\r
- {\r
- psh_glyph_done( ps_debug_glyph );\r
- FT_FREE( ps_debug_glyph );\r
- }\r
-\r
- if ( FT_NEW( glyph ) )\r
- return error;\r
-\r
- ps_debug_glyph = glyph;\r
-\r
-#endif /* DEBUG_HINTER */\r
-\r
- error = psh_glyph_init( glyph, outline, ps_hints, globals );\r
- if ( error )\r
- goto Exit;\r
-\r
- /* try to optimize the y_scale so that the top of non-capital letters\r
- * is aligned on a pixel boundary whenever possible\r
- */\r
- {\r
- PSH_Dimension dim_x = &glyph->globals->dimension[0];\r
- PSH_Dimension dim_y = &glyph->globals->dimension[1];\r
-\r
- FT_Fixed x_scale = dim_x->scale_mult;\r
- FT_Fixed y_scale = dim_y->scale_mult;\r
-\r
- FT_Fixed scaled;\r
- FT_Fixed fitted;\r
-\r
-\r
- scaled = FT_MulFix( globals->blues.normal_top.zones->org_ref, y_scale );\r
- fitted = FT_PIX_ROUND( scaled );\r
-\r
- if ( fitted != 0 && scaled != fitted )\r
- {\r
- y_scale = FT_MulDiv( y_scale, fitted, scaled );\r
-\r
- if ( fitted < scaled )\r
- x_scale -= x_scale / 50;\r
-\r
- psh_globals_set_scale( glyph->globals, x_scale, y_scale, 0, 0 );\r
- }\r
- }\r
-\r
- glyph->do_horz_hints = 1;\r
- glyph->do_vert_hints = 1;\r
-\r
- glyph->do_horz_snapping = FT_BOOL( hint_mode == FT_RENDER_MODE_MONO ||\r
- hint_mode == FT_RENDER_MODE_LCD );\r
-\r
- glyph->do_vert_snapping = FT_BOOL( hint_mode == FT_RENDER_MODE_MONO ||\r
- hint_mode == FT_RENDER_MODE_LCD_V );\r
-\r
- glyph->do_stem_adjust = FT_BOOL( hint_mode != FT_RENDER_MODE_LIGHT );\r
-\r
- for ( dimension = 0; dimension < 2; dimension++ )\r
- {\r
- /* load outline coordinates into glyph */\r
- psh_glyph_load_points( glyph, dimension );\r
-\r
- /* compute local extrema */\r
- psh_glyph_compute_extrema( glyph );\r
-\r
- /* compute aligned stem/hints positions */\r
- psh_hint_table_align_hints( &glyph->hint_tables[dimension],\r
- glyph->globals,\r
- dimension,\r
- glyph );\r
-\r
- /* find strong points, align them, then interpolate others */\r
- psh_glyph_find_strong_points( glyph, dimension );\r
- if ( dimension == 1 )\r
- psh_glyph_find_blue_points( &globals->blues, glyph );\r
- psh_glyph_interpolate_strong_points( glyph, dimension );\r
- psh_glyph_interpolate_normal_points( glyph, dimension );\r
- psh_glyph_interpolate_other_points( glyph, dimension );\r
-\r
- /* save hinted coordinates back to outline */\r
- psh_glyph_save_points( glyph, dimension );\r
- }\r
-\r
- Exit:\r
-\r
-#ifndef DEBUG_HINTER\r
- psh_glyph_done( glyph );\r
-#endif\r
-\r
- return error;\r
- }\r
-\r
-\r
-/* END */\r
+/***************************************************************************/
+/* */
+/* pshalgo.c */
+/* */
+/* PostScript hinting algorithm (body). */
+/* */
+/* Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007 by */
+/* 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. */
+/* */
+/***************************************************************************/
+
+
+#include <ft2build.h>
+#include FT_INTERNAL_OBJECTS_H
+#include FT_INTERNAL_DEBUG_H
+#include FT_INTERNAL_CALC_H
+#include "pshalgo.h"
+
+#include "pshnterr.h"
+
+
+#undef FT_COMPONENT
+#define FT_COMPONENT trace_pshalgo2
+
+
+#ifdef DEBUG_HINTER
+ PSH_Hint_Table ps_debug_hint_table = 0;
+ PSH_HintFunc ps_debug_hint_func = 0;
+ PSH_Glyph ps_debug_glyph = 0;
+#endif
+
+
+#define COMPUTE_INFLEXS /* compute inflection points to optimize `S' */
+ /* and similar glyphs */
+#define STRONGER /* slightly increase the contrast of smooth */
+ /* hinting */
+
+
+ /*************************************************************************/
+ /*************************************************************************/
+ /***** *****/
+ /***** BASIC HINTS RECORDINGS *****/
+ /***** *****/
+ /*************************************************************************/
+ /*************************************************************************/
+
+ /* return true if two stem hints overlap */
+ static FT_Int
+ psh_hint_overlap( PSH_Hint hint1,
+ PSH_Hint hint2 )
+ {
+ return hint1->org_pos + hint1->org_len >= hint2->org_pos &&
+ hint2->org_pos + hint2->org_len >= hint1->org_pos;
+ }
+
+
+ /* destroy hints table */
+ static void
+ psh_hint_table_done( PSH_Hint_Table table,
+ FT_Memory memory )
+ {
+ FT_FREE( table->zones );
+ table->num_zones = 0;
+ table->zone = 0;
+
+ FT_FREE( table->sort );
+ FT_FREE( table->hints );
+ table->num_hints = 0;
+ table->max_hints = 0;
+ table->sort_global = 0;
+ }
+
+
+ /* deactivate all hints in a table */
+ static void
+ psh_hint_table_deactivate( PSH_Hint_Table table )
+ {
+ FT_UInt count = table->max_hints;
+ PSH_Hint hint = table->hints;
+
+
+ for ( ; count > 0; count--, hint++ )
+ {
+ psh_hint_deactivate( hint );
+ hint->order = -1;
+ }
+ }
+
+
+ /* internal function to record a new hint */
+ static void
+ psh_hint_table_record( PSH_Hint_Table table,
+ FT_UInt idx )
+ {
+ PSH_Hint hint = table->hints + idx;
+
+
+ if ( idx >= table->max_hints )
+ {
+ FT_ERROR(( "psh_hint_table_record: invalid hint index %d\n", idx ));
+ return;
+ }
+
+ /* ignore active hints */
+ if ( psh_hint_is_active( hint ) )
+ return;
+
+ psh_hint_activate( hint );
+
+ /* now scan the current active hint set to check */
+ /* whether `hint' overlaps with another hint */
+ {
+ PSH_Hint* sorted = table->sort_global;
+ FT_UInt count = table->num_hints;
+ PSH_Hint hint2;
+
+
+ hint->parent = 0;
+ for ( ; count > 0; count--, sorted++ )
+ {
+ hint2 = sorted[0];
+
+ if ( psh_hint_overlap( hint, hint2 ) )
+ {
+ hint->parent = hint2;
+ break;
+ }
+ }
+ }
+
+ if ( table->num_hints < table->max_hints )
+ table->sort_global[table->num_hints++] = hint;
+ else
+ FT_ERROR(( "psh_hint_table_record: too many sorted hints! BUG!\n" ));
+ }
+
+
+ static void
+ psh_hint_table_record_mask( PSH_Hint_Table table,
+ PS_Mask hint_mask )
+ {
+ FT_Int mask = 0, val = 0;
+ FT_Byte* cursor = hint_mask->bytes;
+ FT_UInt idx, limit;
+
+
+ limit = hint_mask->num_bits;
+
+ for ( idx = 0; idx < limit; idx++ )
+ {
+ if ( mask == 0 )
+ {
+ val = *cursor++;
+ mask = 0x80;
+ }
+
+ if ( val & mask )
+ psh_hint_table_record( table, idx );
+
+ mask >>= 1;
+ }
+ }
+
+
+ /* create hints table */
+ static FT_Error
+ psh_hint_table_init( PSH_Hint_Table table,
+ PS_Hint_Table hints,
+ PS_Mask_Table hint_masks,
+ PS_Mask_Table counter_masks,
+ FT_Memory memory )
+ {
+ FT_UInt count;
+ FT_Error error;
+
+ FT_UNUSED( counter_masks );
+
+
+ count = hints->num_hints;
+
+ /* allocate our tables */
+ if ( FT_NEW_ARRAY( table->sort, 2 * count ) ||
+ FT_NEW_ARRAY( table->hints, count ) ||
+ FT_NEW_ARRAY( table->zones, 2 * count + 1 ) )
+ goto Exit;
+
+ table->max_hints = count;
+ table->sort_global = table->sort + count;
+ table->num_hints = 0;
+ table->num_zones = 0;
+ table->zone = 0;
+
+ /* initialize the `table->hints' array */
+ {
+ PSH_Hint write = table->hints;
+ PS_Hint read = hints->hints;
+
+
+ for ( ; count > 0; count--, write++, read++ )
+ {
+ write->org_pos = read->pos;
+ write->org_len = read->len;
+ write->flags = read->flags;
+ }
+ }
+
+ /* we now need to determine the initial `parent' stems; first */
+ /* activate the hints that are given by the initial hint masks */
+ if ( hint_masks )
+ {
+ PS_Mask mask = hint_masks->masks;
+
+
+ count = hint_masks->num_masks;
+ table->hint_masks = hint_masks;
+
+ for ( ; count > 0; count--, mask++ )
+ psh_hint_table_record_mask( table, mask );
+ }
+
+ /* finally, do a linear parse in case some hints were left alone */
+ if ( table->num_hints != table->max_hints )
+ {
+ FT_UInt idx;
+
+
+ FT_ERROR(( "psh_hint_table_init: missing/incorrect hint masks!\n" ));
+
+ count = table->max_hints;
+ for ( idx = 0; idx < count; idx++ )
+ psh_hint_table_record( table, idx );
+ }
+
+ Exit:
+ return error;
+ }
+
+
+ static void
+ psh_hint_table_activate_mask( PSH_Hint_Table table,
+ PS_Mask hint_mask )
+ {
+ FT_Int mask = 0, val = 0;
+ FT_Byte* cursor = hint_mask->bytes;
+ FT_UInt idx, limit, count;
+
+
+ limit = hint_mask->num_bits;
+ count = 0;
+
+ psh_hint_table_deactivate( table );
+
+ for ( idx = 0; idx < limit; idx++ )
+ {
+ if ( mask == 0 )
+ {
+ val = *cursor++;
+ mask = 0x80;
+ }
+
+ if ( val & mask )
+ {
+ PSH_Hint hint = &table->hints[idx];
+
+
+ if ( !psh_hint_is_active( hint ) )
+ {
+ FT_UInt count2;
+
+#if 0
+ PSH_Hint* sort = table->sort;
+ PSH_Hint hint2;
+
+
+ for ( count2 = count; count2 > 0; count2--, sort++ )
+ {
+ hint2 = sort[0];
+ if ( psh_hint_overlap( hint, hint2 ) )
+ FT_ERROR(( "psh_hint_table_activate_mask:"
+ " found overlapping hints\n" ))
+ }
+#else
+ count2 = 0;
+#endif
+
+ if ( count2 == 0 )
+ {
+ psh_hint_activate( hint );
+ if ( count < table->max_hints )
+ table->sort[count++] = hint;
+ else
+ FT_ERROR(( "psh_hint_tableactivate_mask:"
+ " too many active hints\n" ));
+ }
+ }
+ }
+
+ mask >>= 1;
+ }
+ table->num_hints = count;
+
+ /* now, sort the hints; they are guaranteed to not overlap */
+ /* so we can compare their "org_pos" field directly */
+ {
+ FT_Int i1, i2;
+ PSH_Hint hint1, hint2;
+ PSH_Hint* sort = table->sort;
+
+
+ /* a simple bubble sort will do, since in 99% of cases, the hints */
+ /* will be already sorted -- and the sort will be linear */
+ for ( i1 = 1; i1 < (FT_Int)count; i1++ )
+ {
+ hint1 = sort[i1];
+ for ( i2 = i1 - 1; i2 >= 0; i2-- )
+ {
+ hint2 = sort[i2];
+
+ if ( hint2->org_pos < hint1->org_pos )
+ break;
+
+ sort[i2 + 1] = hint2;
+ sort[i2] = hint1;
+ }
+ }
+ }
+ }
+
+
+ /*************************************************************************/
+ /*************************************************************************/
+ /***** *****/
+ /***** HINTS GRID-FITTING AND OPTIMIZATION *****/
+ /***** *****/
+ /*************************************************************************/
+ /*************************************************************************/
+
+#if 1
+ static FT_Pos
+ psh_dimension_quantize_len( PSH_Dimension dim,
+ FT_Pos len,
+ FT_Bool do_snapping )
+ {
+ if ( len <= 64 )
+ len = 64;
+ else
+ {
+ FT_Pos delta = len - dim->stdw.widths[0].cur;
+
+
+ if ( delta < 0 )
+ delta = -delta;
+
+ if ( delta < 40 )
+ {
+ len = dim->stdw.widths[0].cur;
+ if ( len < 48 )
+ len = 48;
+ }
+
+ if ( len < 3 * 64 )
+ {
+ delta = ( len & 63 );
+ len &= -64;
+
+ if ( delta < 10 )
+ len += delta;
+
+ else if ( delta < 32 )
+ len += 10;
+
+ else if ( delta < 54 )
+ len += 54;
+
+ else
+ len += delta;
+ }
+ else
+ len = FT_PIX_ROUND( len );
+ }
+
+ if ( do_snapping )
+ len = FT_PIX_ROUND( len );
+
+ return len;
+ }
+#endif /* 0 */
+
+
+#ifdef DEBUG_HINTER
+
+ static void
+ ps_simple_scale( PSH_Hint_Table table,
+ FT_Fixed scale,
+ FT_Fixed delta,
+ FT_Int dimension )
+ {
+ PSH_Hint hint;
+ FT_UInt count;
+
+
+ for ( count = 0; count < table->max_hints; count++ )
+ {
+ hint = table->hints + count;
+
+ hint->cur_pos = FT_MulFix( hint->org_pos, scale ) + delta;
+ hint->cur_len = FT_MulFix( hint->org_len, scale );
+
+ if ( ps_debug_hint_func )
+ ps_debug_hint_func( hint, dimension );
+ }
+ }
+
+#endif /* DEBUG_HINTER */
+
+
+ static FT_Fixed
+ psh_hint_snap_stem_side_delta( FT_Fixed pos,
+ FT_Fixed len )
+ {
+ FT_Fixed delta1 = FT_PIX_ROUND( pos ) - pos;
+ FT_Fixed delta2 = FT_PIX_ROUND( pos + len ) - pos - len;
+
+
+ if ( FT_ABS( delta1 ) <= FT_ABS( delta2 ) )
+ return delta1;
+ else
+ return delta2;
+ }
+
+
+ static void
+ psh_hint_align( PSH_Hint hint,
+ PSH_Globals globals,
+ FT_Int dimension,
+ PSH_Glyph glyph )
+ {
+ PSH_Dimension dim = &globals->dimension[dimension];
+ FT_Fixed scale = dim->scale_mult;
+ FT_Fixed delta = dim->scale_delta;
+
+
+ if ( !psh_hint_is_fitted( hint ) )
+ {
+ FT_Pos pos = FT_MulFix( hint->org_pos, scale ) + delta;
+ FT_Pos len = FT_MulFix( hint->org_len, scale );
+
+ FT_Int do_snapping;
+ FT_Pos fit_len;
+ PSH_AlignmentRec align;
+
+
+ /* ignore stem alignments when requested through the hint flags */
+ if ( ( dimension == 0 && !glyph->do_horz_hints ) ||
+ ( dimension == 1 && !glyph->do_vert_hints ) )
+ {
+ hint->cur_pos = pos;
+ hint->cur_len = len;
+
+ psh_hint_set_fitted( hint );
+ return;
+ }
+
+ /* perform stem snapping when requested - this is necessary
+ * for monochrome and LCD hinting modes only
+ */
+ do_snapping = ( dimension == 0 && glyph->do_horz_snapping ) ||
+ ( dimension == 1 && glyph->do_vert_snapping );
+
+ hint->cur_len = fit_len = len;
+
+ /* check blue zones for horizontal stems */
+ align.align = PSH_BLUE_ALIGN_NONE;
+ align.align_bot = align.align_top = 0;
+
+ if ( dimension == 1 )
+ psh_blues_snap_stem( &globals->blues,
+ hint->org_pos + hint->org_len,
+ hint->org_pos,
+ &align );
+
+ switch ( align.align )
+ {
+ case PSH_BLUE_ALIGN_TOP:
+ /* the top of the stem is aligned against a blue zone */
+ hint->cur_pos = align.align_top - fit_len;
+ break;
+
+ case PSH_BLUE_ALIGN_BOT:
+ /* the bottom of the stem is aligned against a blue zone */
+ hint->cur_pos = align.align_bot;
+ break;
+
+ case PSH_BLUE_ALIGN_TOP | PSH_BLUE_ALIGN_BOT:
+ /* both edges of the stem are aligned against blue zones */
+ hint->cur_pos = align.align_bot;
+ hint->cur_len = align.align_top - align.align_bot;
+ break;
+
+ default:
+ {
+ PSH_Hint parent = hint->parent;
+
+
+ if ( parent )
+ {
+ FT_Pos par_org_center, par_cur_center;
+ FT_Pos cur_org_center, cur_delta;
+
+
+ /* ensure that parent is already fitted */
+ if ( !psh_hint_is_fitted( parent ) )
+ psh_hint_align( parent, globals, dimension, glyph );
+
+ /* keep original relation between hints, this is, use the */
+ /* scaled distance between the centers of the hints to */
+ /* compute the new position */
+ par_org_center = parent->org_pos + ( parent->org_len >> 1 );
+ par_cur_center = parent->cur_pos + ( parent->cur_len >> 1 );
+ cur_org_center = hint->org_pos + ( hint->org_len >> 1 );
+
+ cur_delta = FT_MulFix( cur_org_center - par_org_center, scale );
+ pos = par_cur_center + cur_delta - ( len >> 1 );
+ }
+
+ hint->cur_pos = pos;
+ hint->cur_len = fit_len;
+
+ /* Stem adjustment tries to snap stem widths to standard
+ * ones. This is important to prevent unpleasant rounding
+ * artefacts.
+ */
+ if ( glyph->do_stem_adjust )
+ {
+ if ( len <= 64 )
+ {
+ /* the stem is less than one pixel; we will center it
+ * around the nearest pixel center
+ */
+ if ( len >= 32 )
+ {
+ /* This is a special case where we also widen the stem
+ * and align it to the pixel grid.
+ *
+ * stem_center = pos + (len/2)
+ * nearest_pixel_center = FT_ROUND(stem_center-32)+32
+ * new_pos = nearest_pixel_center-32
+ * = FT_ROUND(stem_center-32)
+ * = FT_FLOOR(stem_center-32+32)
+ * = FT_FLOOR(stem_center)
+ * new_len = 64
+ */
+ pos = FT_PIX_FLOOR( pos + ( len >> 1 ) );
+ len = 64;
+ }
+ else if ( len > 0 )
+ {
+ /* This is a very small stem; we simply align it to the
+ * pixel grid, trying to find the minimal displacement.
+ *
+ * left = pos
+ * right = pos + len
+ * left_nearest_edge = ROUND(pos)
+ * right_nearest_edge = ROUND(right)
+ *
+ * if ( ABS(left_nearest_edge - left) <=
+ * ABS(right_nearest_edge - right) )
+ * new_pos = left
+ * else
+ * new_pos = right
+ */
+ FT_Pos left_nearest = FT_PIX_ROUND( pos );
+ FT_Pos right_nearest = FT_PIX_ROUND( pos + len );
+ FT_Pos left_disp = left_nearest - pos;
+ FT_Pos right_disp = right_nearest - ( pos + len );
+
+
+ if ( left_disp < 0 )
+ left_disp = -left_disp;
+ if ( right_disp < 0 )
+ right_disp = -right_disp;
+ if ( left_disp <= right_disp )
+ pos = left_nearest;
+ else
+ pos = right_nearest;
+ }
+ else
+ {
+ /* this is a ghost stem; we simply round it */
+ pos = FT_PIX_ROUND( pos );
+ }
+ }
+ else
+ {
+ len = psh_dimension_quantize_len( dim, len, 0 );
+ }
+ }
+
+ /* now that we have a good hinted stem width, try to position */
+ /* the stem along a pixel grid integer coordinate */
+ hint->cur_pos = pos + psh_hint_snap_stem_side_delta( pos, len );
+ hint->cur_len = len;
+ }
+ }
+
+ if ( do_snapping )
+ {
+ pos = hint->cur_pos;
+ len = hint->cur_len;
+
+ if ( len < 64 )
+ len = 64;
+ else
+ len = FT_PIX_ROUND( len );
+
+ switch ( align.align )
+ {
+ case PSH_BLUE_ALIGN_TOP:
+ hint->cur_pos = align.align_top - len;
+ hint->cur_len = len;
+ break;
+
+ case PSH_BLUE_ALIGN_BOT:
+ hint->cur_len = len;
+ break;
+
+ case PSH_BLUE_ALIGN_BOT | PSH_BLUE_ALIGN_TOP:
+ /* don't touch */
+ break;
+
+
+ default:
+ hint->cur_len = len;
+ if ( len & 64 )
+ pos = FT_PIX_FLOOR( pos + ( len >> 1 ) ) + 32;
+ else
+ pos = FT_PIX_ROUND( pos + ( len >> 1 ) );
+
+ hint->cur_pos = pos - ( len >> 1 );
+ hint->cur_len = len;
+ }
+ }
+
+ psh_hint_set_fitted( hint );
+
+#ifdef DEBUG_HINTER
+ if ( ps_debug_hint_func )
+ ps_debug_hint_func( hint, dimension );
+#endif
+ }
+ }
+
+
+#if 0 /* not used for now, experimental */
+
+ /*
+ * A variant to perform "light" hinting (i.e. FT_RENDER_MODE_LIGHT)
+ * of stems
+ */
+ static void
+ psh_hint_align_light( PSH_Hint hint,
+ PSH_Globals globals,
+ FT_Int dimension,
+ PSH_Glyph glyph )
+ {
+ PSH_Dimension dim = &globals->dimension[dimension];
+ FT_Fixed scale = dim->scale_mult;
+ FT_Fixed delta = dim->scale_delta;
+
+
+ if ( !psh_hint_is_fitted( hint ) )
+ {
+ FT_Pos pos = FT_MulFix( hint->org_pos, scale ) + delta;
+ FT_Pos len = FT_MulFix( hint->org_len, scale );
+
+ FT_Pos fit_len;
+
+ PSH_AlignmentRec align;
+
+
+ /* ignore stem alignments when requested through the hint flags */
+ if ( ( dimension == 0 && !glyph->do_horz_hints ) ||
+ ( dimension == 1 && !glyph->do_vert_hints ) )
+ {
+ hint->cur_pos = pos;
+ hint->cur_len = len;
+
+ psh_hint_set_fitted( hint );
+ return;
+ }
+
+ fit_len = len;
+
+ hint->cur_len = fit_len;
+
+ /* check blue zones for horizontal stems */
+ align.align = PSH_BLUE_ALIGN_NONE;
+ align.align_bot = align.align_top = 0;
+
+ if ( dimension == 1 )
+ psh_blues_snap_stem( &globals->blues,
+ hint->org_pos + hint->org_len,
+ hint->org_pos,
+ &align );
+
+ switch ( align.align )
+ {
+ case PSH_BLUE_ALIGN_TOP:
+ /* the top of the stem is aligned against a blue zone */
+ hint->cur_pos = align.align_top - fit_len;
+ break;
+
+ case PSH_BLUE_ALIGN_BOT:
+ /* the bottom of the stem is aligned against a blue zone */
+ hint->cur_pos = align.align_bot;
+ break;
+
+ case PSH_BLUE_ALIGN_TOP | PSH_BLUE_ALIGN_BOT:
+ /* both edges of the stem are aligned against blue zones */
+ hint->cur_pos = align.align_bot;
+ hint->cur_len = align.align_top - align.align_bot;
+ break;
+
+ default:
+ {
+ PSH_Hint parent = hint->parent;
+
+
+ if ( parent )
+ {
+ FT_Pos par_org_center, par_cur_center;
+ FT_Pos cur_org_center, cur_delta;
+
+
+ /* ensure that parent is already fitted */
+ if ( !psh_hint_is_fitted( parent ) )
+ psh_hint_align_light( parent, globals, dimension, glyph );
+
+ par_org_center = parent->org_pos + ( parent->org_len / 2 );
+ par_cur_center = parent->cur_pos + ( parent->cur_len / 2 );
+ cur_org_center = hint->org_pos + ( hint->org_len / 2 );
+
+ cur_delta = FT_MulFix( cur_org_center - par_org_center, scale );
+ pos = par_cur_center + cur_delta - ( len >> 1 );
+ }
+
+ /* Stems less than one pixel wide are easy -- we want to
+ * make them as dark as possible, so they must fall within
+ * one pixel. If the stem is split between two pixels
+ * then snap the edge that is nearer to the pixel boundary
+ * to the pixel boundary.
+ */
+ if ( len <= 64 )
+ {
+ if ( ( pos + len + 63 ) / 64 != pos / 64 + 1 )
+ pos += psh_hint_snap_stem_side_delta ( pos, len );
+ }
+
+ /* Position stems other to minimize the amount of mid-grays.
+ * There are, in general, two positions that do this,
+ * illustrated as A) and B) below.
+ *
+ * + + + +
+ *
+ * A) |--------------------------------|
+ * B) |--------------------------------|
+ * C) |--------------------------------|
+ *
+ * Position A) (split the excess stem equally) should be better
+ * for stems of width N + f where f < 0.5.
+ *
+ * Position B) (split the deficiency equally) should be better
+ * for stems of width N + f where f > 0.5.
+ *
+ * It turns out though that minimizing the total number of lit
+ * pixels is also important, so position C), with one edge
+ * aligned with a pixel boundary is actually preferable
+ * to A). There are also more possibile positions for C) than
+ * for A) or B), so it involves less distortion of the overall
+ * character shape.
+ */
+ else /* len > 64 */
+ {
+ FT_Fixed frac_len = len & 63;
+ FT_Fixed center = pos + ( len >> 1 );
+ FT_Fixed delta_a, delta_b;
+
+
+ if ( ( len / 64 ) & 1 )
+ {
+ delta_a = FT_PIX_FLOOR( center ) + 32 - center;
+ delta_b = FT_PIX_ROUND( center ) - center;
+ }
+ else
+ {
+ delta_a = FT_PIX_ROUND( center ) - center;
+ delta_b = FT_PIX_FLOOR( center ) + 32 - center;
+ }
+
+ /* We choose between B) and C) above based on the amount
+ * of fractinal stem width; for small amounts, choose
+ * C) always, for large amounts, B) always, and inbetween,
+ * pick whichever one involves less stem movement.
+ */
+ if ( frac_len < 32 )
+ {
+ pos += psh_hint_snap_stem_side_delta ( pos, len );
+ }
+ else if ( frac_len < 48 )
+ {
+ FT_Fixed side_delta = psh_hint_snap_stem_side_delta ( pos,
+ len );
+
+ if ( FT_ABS( side_delta ) < FT_ABS( delta_b ) )
+ pos += side_delta;
+ else
+ pos += delta_b;
+ }
+ else
+ {
+ pos += delta_b;
+ }
+ }
+
+ hint->cur_pos = pos;
+ }
+ } /* switch */
+
+ psh_hint_set_fitted( hint );
+
+#ifdef DEBUG_HINTER
+ if ( ps_debug_hint_func )
+ ps_debug_hint_func( hint, dimension );
+#endif
+ }
+ }
+
+#endif /* 0 */
+
+
+ static void
+ psh_hint_table_align_hints( PSH_Hint_Table table,
+ PSH_Globals globals,
+ FT_Int dimension,
+ PSH_Glyph glyph )
+ {
+ PSH_Hint hint;
+ FT_UInt count;
+
+#ifdef DEBUG_HINTER
+
+ PSH_Dimension dim = &globals->dimension[dimension];
+ FT_Fixed scale = dim->scale_mult;
+ FT_Fixed delta = dim->scale_delta;
+
+
+ if ( ps_debug_no_vert_hints && dimension == 0 )
+ {
+ ps_simple_scale( table, scale, delta, dimension );
+ return;
+ }
+
+ if ( ps_debug_no_horz_hints && dimension == 1 )
+ {
+ ps_simple_scale( table, scale, delta, dimension );
+ return;
+ }
+
+#endif /* DEBUG_HINTER*/
+
+ hint = table->hints;
+ count = table->max_hints;
+
+ for ( ; count > 0; count--, hint++ )
+ psh_hint_align( hint, globals, dimension, glyph );
+ }
+
+
+ /*************************************************************************/
+ /*************************************************************************/
+ /***** *****/
+ /***** POINTS INTERPOLATION ROUTINES *****/
+ /***** *****/
+ /*************************************************************************/
+ /*************************************************************************/
+
+#define PSH_ZONE_MIN -3200000L
+#define PSH_ZONE_MAX +3200000L
+
+#define xxDEBUG_ZONES
+
+
+#ifdef DEBUG_ZONES
+
+#include <stdio.h>
+
+ static void
+ psh_print_zone( PSH_Zone zone )
+ {
+ printf( "zone [scale,delta,min,max] = [%.3f,%.3f,%d,%d]\n",
+ zone->scale / 65536.0,
+ zone->delta / 64.0,
+ zone->min,
+ zone->max );
+ }
+
+#else
+
+#define psh_print_zone( x ) do { } while ( 0 )
+
+#endif /* DEBUG_ZONES */
+
+
+ /*************************************************************************/
+ /*************************************************************************/
+ /***** *****/
+ /***** HINTER GLYPH MANAGEMENT *****/
+ /***** *****/
+ /*************************************************************************/
+ /*************************************************************************/
+
+#if 1
+
+#define psh_corner_is_flat ft_corner_is_flat
+#define psh_corner_orientation ft_corner_orientation
+
+#else
+
+ FT_LOCAL_DEF( FT_Int )
+ psh_corner_is_flat( FT_Pos x_in,
+ FT_Pos y_in,
+ FT_Pos x_out,
+ FT_Pos y_out )
+ {
+ FT_Pos ax = x_in;
+ FT_Pos ay = y_in;
+
+ FT_Pos d_in, d_out, d_corner;
+
+
+ if ( ax < 0 )
+ ax = -ax;
+ if ( ay < 0 )
+ ay = -ay;
+ d_in = ax + ay;
+
+ ax = x_out;
+ if ( ax < 0 )
+ ax = -ax;
+ ay = y_out;
+ if ( ay < 0 )
+ ay = -ay;
+ d_out = ax + ay;
+
+ ax = x_out + x_in;
+ if ( ax < 0 )
+ ax = -ax;
+ ay = y_out + y_in;
+ if ( ay < 0 )
+ ay = -ay;
+ d_corner = ax + ay;
+
+ return ( d_in + d_out - d_corner ) < ( d_corner >> 4 );
+ }
+
+ static FT_Int
+ psh_corner_orientation( FT_Pos in_x,
+ FT_Pos in_y,
+ FT_Pos out_x,
+ FT_Pos out_y )
+ {
+ FT_Int result;
+
+
+ /* deal with the trivial cases quickly */
+ if ( in_y == 0 )
+ {
+ if ( in_x >= 0 )
+ result = out_y;
+ else
+ result = -out_y;
+ }
+ else if ( in_x == 0 )
+ {
+ if ( in_y >= 0 )
+ result = -out_x;
+ else
+ result = out_x;
+ }
+ else if ( out_y == 0 )
+ {
+ if ( out_x >= 0 )
+ result = in_y;
+ else
+ result = -in_y;
+ }
+ else if ( out_x == 0 )
+ {
+ if ( out_y >= 0 )
+ result = -in_x;
+ else
+ result = in_x;
+ }
+ else /* general case */
+ {
+ long long delta = (long long)in_x * out_y - (long long)in_y * out_x;
+
+ if ( delta == 0 )
+ result = 0;
+ else
+ result = 1 - 2 * ( delta < 0 );
+ }
+
+ return result;
+ }
+
+#endif /* !1 */
+
+
+#ifdef COMPUTE_INFLEXS
+
+ /* compute all inflex points in a given glyph */
+ static void
+ psh_glyph_compute_inflections( PSH_Glyph glyph )
+ {
+ FT_UInt n;
+
+
+ for ( n = 0; n < glyph->num_contours; n++ )
+ {
+ PSH_Point first, start, end, before, after;
+ FT_Pos in_x, in_y, out_x, out_y;
+ FT_Int orient_prev, orient_cur;
+ FT_Int finished = 0;
+
+
+ /* we need at least 4 points to create an inflection point */
+ if ( glyph->contours[n].count < 4 )
+ continue;
+
+ /* compute first segment in contour */
+ first = glyph->contours[n].start;
+
+ start = end = first;
+ do
+ {
+ end = end->next;
+ if ( end == first )
+ goto Skip;
+
+ in_x = end->org_u - start->org_u;
+ in_y = end->org_v - start->org_v;
+
+ } while ( in_x == 0 && in_y == 0 );
+
+ /* extend the segment start whenever possible */
+ before = start;
+ do
+ {
+ do
+ {
+ start = before;
+ before = before->prev;
+ if ( before == first )
+ goto Skip;
+
+ out_x = start->org_u - before->org_u;
+ out_y = start->org_v - before->org_v;
+
+ } while ( out_x == 0 && out_y == 0 );
+
+ orient_prev = psh_corner_orientation( in_x, in_y, out_x, out_y );
+
+ } while ( orient_prev == 0 );
+
+ first = start;
+ in_x = out_x;
+ in_y = out_y;
+
+ /* now, process all segments in the contour */
+ do
+ {
+ /* first, extend current segment's end whenever possible */
+ after = end;
+ do
+ {
+ do
+ {
+ end = after;
+ after = after->next;
+ if ( after == first )
+ finished = 1;
+
+ out_x = after->org_u - end->org_u;
+ out_y = after->org_v - end->org_v;
+
+ } while ( out_x == 0 && out_y == 0 );
+
+ orient_cur = psh_corner_orientation( in_x, in_y, out_x, out_y );
+
+ } while ( orient_cur == 0 );
+
+ if ( ( orient_cur ^ orient_prev ) < 0 )
+ {
+ do
+ {
+ psh_point_set_inflex( start );
+ start = start->next;
+ }
+ while ( start != end );
+
+ psh_point_set_inflex( start );
+ }
+
+ start = end;
+ end = after;
+ orient_prev = orient_cur;
+ in_x = out_x;
+ in_y = out_y;
+
+ } while ( !finished );
+
+ Skip:
+ ;
+ }
+ }
+
+#endif /* COMPUTE_INFLEXS */
+
+
+ static void
+ psh_glyph_done( PSH_Glyph glyph )
+ {
+ FT_Memory memory = glyph->memory;
+
+
+ psh_hint_table_done( &glyph->hint_tables[1], memory );
+ psh_hint_table_done( &glyph->hint_tables[0], memory );
+
+ FT_FREE( glyph->points );
+ FT_FREE( glyph->contours );
+
+ glyph->num_points = 0;
+ glyph->num_contours = 0;
+
+ glyph->memory = 0;
+ }
+
+
+ static int
+ psh_compute_dir( FT_Pos dx,
+ FT_Pos dy )
+ {
+ FT_Pos ax, ay;
+ int result = PSH_DIR_NONE;
+
+
+ ax = ( dx >= 0 ) ? dx : -dx;
+ ay = ( dy >= 0 ) ? dy : -dy;
+
+ if ( ay * 12 < ax )
+ {
+ /* |dy| <<< |dx| means a near-horizontal segment */
+ result = ( dx >= 0 ) ? PSH_DIR_RIGHT : PSH_DIR_LEFT;
+ }
+ else if ( ax * 12 < ay )
+ {
+ /* |dx| <<< |dy| means a near-vertical segment */
+ result = ( dy >= 0 ) ? PSH_DIR_UP : PSH_DIR_DOWN;
+ }
+
+ return result;
+ }
+
+
+ /* load outline point coordinates into hinter glyph */
+ static void
+ psh_glyph_load_points( PSH_Glyph glyph,
+ FT_Int dimension )
+ {
+ FT_Vector* vec = glyph->outline->points;
+ PSH_Point point = glyph->points;
+ FT_UInt count = glyph->num_points;
+
+
+ for ( ; count > 0; count--, point++, vec++ )
+ {
+ point->flags2 = 0;
+ point->hint = NULL;
+ if ( dimension == 0 )
+ {
+ point->org_u = vec->x;
+ point->org_v = vec->y;
+ }
+ else
+ {
+ point->org_u = vec->y;
+ point->org_v = vec->x;
+ }
+
+#ifdef DEBUG_HINTER
+ point->org_x = vec->x;
+ point->org_y = vec->y;
+#endif
+
+ }
+ }
+
+
+ /* save hinted point coordinates back to outline */
+ static void
+ psh_glyph_save_points( PSH_Glyph glyph,
+ FT_Int dimension )
+ {
+ FT_UInt n;
+ PSH_Point point = glyph->points;
+ FT_Vector* vec = glyph->outline->points;
+ char* tags = glyph->outline->tags;
+
+
+ for ( n = 0; n < glyph->num_points; n++ )
+ {
+ if ( dimension == 0 )
+ vec[n].x = point->cur_u;
+ else
+ vec[n].y = point->cur_u;
+
+ if ( psh_point_is_strong( point ) )
+ tags[n] |= (char)( ( dimension == 0 ) ? 32 : 64 );
+
+#ifdef DEBUG_HINTER
+
+ if ( dimension == 0 )
+ {
+ point->cur_x = point->cur_u;
+ point->flags_x = point->flags2 | point->flags;
+ }
+ else
+ {
+ point->cur_y = point->cur_u;
+ point->flags_y = point->flags2 | point->flags;
+ }
+
+#endif
+
+ point++;
+ }
+ }
+
+
+ static FT_Error
+ psh_glyph_init( PSH_Glyph glyph,
+ FT_Outline* outline,
+ PS_Hints ps_hints,
+ PSH_Globals globals )
+ {
+ FT_Error error;
+ FT_Memory memory;
+
+
+ /* clear all fields */
+ FT_MEM_ZERO( glyph, sizeof ( *glyph ) );
+
+ memory = glyph->memory = globals->memory;
+
+ /* allocate and setup points + contours arrays */
+ if ( FT_NEW_ARRAY( glyph->points, outline->n_points ) ||
+ FT_NEW_ARRAY( glyph->contours, outline->n_contours ) )
+ goto Exit;
+
+ glyph->num_points = outline->n_points;
+ glyph->num_contours = outline->n_contours;
+
+ {
+ FT_UInt first = 0, next, n;
+ PSH_Point points = glyph->points;
+ PSH_Contour contour = glyph->contours;
+
+
+ for ( n = 0; n < glyph->num_contours; n++ )
+ {
+ FT_Int count;
+ PSH_Point point;
+
+
+ next = outline->contours[n] + 1;
+ count = next - first;
+
+ contour->start = points + first;
+ contour->count = (FT_UInt)count;
+
+ if ( count > 0 )
+ {
+ point = points + first;
+
+ point->prev = points + next - 1;
+ point->contour = contour;
+
+ for ( ; count > 1; count-- )
+ {
+ point[0].next = point + 1;
+ point[1].prev = point;
+ point++;
+ point->contour = contour;
+ }
+ point->next = points + first;
+ }
+
+ contour++;
+ first = next;
+ }
+ }
+
+ {
+ PSH_Point points = glyph->points;
+ PSH_Point point = points;
+ FT_Vector* vec = outline->points;
+ FT_UInt n;
+
+
+ for ( n = 0; n < glyph->num_points; n++, point++ )
+ {
+ FT_Int n_prev = (FT_Int)( point->prev - points );
+ FT_Int n_next = (FT_Int)( point->next - points );
+ FT_Pos dxi, dyi, dxo, dyo;
+
+
+ if ( !( outline->tags[n] & FT_CURVE_TAG_ON ) )
+ point->flags = PSH_POINT_OFF;
+
+ dxi = vec[n].x - vec[n_prev].x;
+ dyi = vec[n].y - vec[n_prev].y;
+
+ point->dir_in = (FT_Char)psh_compute_dir( dxi, dyi );
+
+ dxo = vec[n_next].x - vec[n].x;
+ dyo = vec[n_next].y - vec[n].y;
+
+ point->dir_out = (FT_Char)psh_compute_dir( dxo, dyo );
+
+ /* detect smooth points */
+ if ( point->flags & PSH_POINT_OFF )
+ point->flags |= PSH_POINT_SMOOTH;
+
+ else if ( point->dir_in == point->dir_out )
+ {
+ if ( point->dir_out != PSH_DIR_NONE ||
+ psh_corner_is_flat( dxi, dyi, dxo, dyo ) )
+ point->flags |= PSH_POINT_SMOOTH;
+ }
+ }
+ }
+
+ glyph->outline = outline;
+ glyph->globals = globals;
+
+#ifdef COMPUTE_INFLEXS
+ psh_glyph_load_points( glyph, 0 );
+ psh_glyph_compute_inflections( glyph );
+#endif /* COMPUTE_INFLEXS */
+
+ /* now deal with hints tables */
+ error = psh_hint_table_init( &glyph->hint_tables [0],
+ &ps_hints->dimension[0].hints,
+ &ps_hints->dimension[0].masks,
+ &ps_hints->dimension[0].counters,
+ memory );
+ if ( error )
+ goto Exit;
+
+ error = psh_hint_table_init( &glyph->hint_tables [1],
+ &ps_hints->dimension[1].hints,
+ &ps_hints->dimension[1].masks,
+ &ps_hints->dimension[1].counters,
+ memory );
+ if ( error )
+ goto Exit;
+
+ Exit:
+ return error;
+ }
+
+
+ /* compute all extrema in a glyph for a given dimension */
+ static void
+ psh_glyph_compute_extrema( PSH_Glyph glyph )
+ {
+ FT_UInt n;
+
+
+ /* first of all, compute all local extrema */
+ for ( n = 0; n < glyph->num_contours; n++ )
+ {
+ PSH_Point first = glyph->contours[n].start;
+ PSH_Point point, before, after;
+
+
+ if ( glyph->contours[n].count == 0 )
+ continue;
+
+ point = first;
+ before = point;
+ after = point;
+
+ do
+ {
+ before = before->prev;
+ if ( before == first )
+ goto Skip;
+
+ } while ( before->org_u == point->org_u );
+
+ first = point = before->next;
+
+ for (;;)
+ {
+ after = point;
+ do
+ {
+ after = after->next;
+ if ( after == first )
+ goto Next;
+
+ } while ( after->org_u == point->org_u );
+
+ if ( before->org_u < point->org_u )
+ {
+ if ( after->org_u < point->org_u )
+ {
+ /* local maximum */
+ goto Extremum;
+ }
+ }
+ else /* before->org_u > point->org_u */
+ {
+ if ( after->org_u > point->org_u )
+ {
+ /* local minimum */
+ Extremum:
+ do
+ {
+ psh_point_set_extremum( point );
+ point = point->next;
+
+ } while ( point != after );
+ }
+ }
+
+ before = after->prev;
+ point = after;
+
+ } /* for */
+
+ Next:
+ ;
+ }
+
+ /* for each extremum, determine its direction along the */
+ /* orthogonal axis */
+ for ( n = 0; n < glyph->num_points; n++ )
+ {
+ PSH_Point point, before, after;
+
+
+ point = &glyph->points[n];
+ before = point;
+ after = point;
+
+ if ( psh_point_is_extremum( point ) )
+ {
+ do
+ {
+ before = before->prev;
+ if ( before == point )
+ goto Skip;
+
+ } while ( before->org_v == point->org_v );
+
+ do
+ {
+ after = after->next;
+ if ( after == point )
+ goto Skip;
+
+ } while ( after->org_v == point->org_v );
+ }
+
+ if ( before->org_v < point->org_v &&
+ after->org_v > point->org_v )
+ {
+ psh_point_set_positive( point );
+ }
+ else if ( before->org_v > point->org_v &&
+ after->org_v < point->org_v )
+ {
+ psh_point_set_negative( point );
+ }
+
+ Skip:
+ ;
+ }
+ }
+
+
+ /* major_dir is the direction for points on the bottom/left of the stem; */
+ /* Points on the top/right of the stem will have a direction of */
+ /* -major_dir. */
+
+ static void
+ psh_hint_table_find_strong_points( PSH_Hint_Table table,
+ PSH_Point point,
+ FT_UInt count,
+ FT_Int threshold,
+ FT_Int major_dir )
+ {
+ PSH_Hint* sort = table->sort;
+ FT_UInt num_hints = table->num_hints;
+
+
+ for ( ; count > 0; count--, point++ )
+ {
+ FT_Int point_dir = 0;
+ FT_Pos org_u = point->org_u;
+
+
+ if ( psh_point_is_strong( point ) )
+ continue;
+
+ if ( PSH_DIR_COMPARE( point->dir_in, major_dir ) )
+ point_dir = point->dir_in;
+
+ else if ( PSH_DIR_COMPARE( point->dir_out, major_dir ) )
+ point_dir = point->dir_out;
+
+ if ( point_dir )
+ {
+ if ( point_dir == major_dir )
+ {
+ FT_UInt nn;
+
+
+ for ( nn = 0; nn < num_hints; nn++ )
+ {
+ PSH_Hint hint = sort[nn];
+ FT_Pos d = org_u - hint->org_pos;
+
+
+ if ( d < threshold && -d < threshold )
+ {
+ psh_point_set_strong( point );
+ point->flags2 |= PSH_POINT_EDGE_MIN;
+ point->hint = hint;
+ break;
+ }
+ }
+ }
+ else if ( point_dir == -major_dir )
+ {
+ FT_UInt nn;
+
+
+ for ( nn = 0; nn < num_hints; nn++ )
+ {
+ PSH_Hint hint = sort[nn];
+ FT_Pos d = org_u - hint->org_pos - hint->org_len;
+
+
+ if ( d < threshold && -d < threshold )
+ {
+ psh_point_set_strong( point );
+ point->flags2 |= PSH_POINT_EDGE_MAX;
+ point->hint = hint;
+ break;
+ }
+ }
+ }
+ }
+
+#if 1
+ else if ( psh_point_is_extremum( point ) )
+ {
+ /* treat extrema as special cases for stem edge alignment */
+ FT_UInt nn, min_flag, max_flag;
+
+
+ if ( major_dir == PSH_DIR_HORIZONTAL )
+ {
+ min_flag = PSH_POINT_POSITIVE;
+ max_flag = PSH_POINT_NEGATIVE;
+ }
+ else
+ {
+ min_flag = PSH_POINT_NEGATIVE;
+ max_flag = PSH_POINT_POSITIVE;
+ }
+
+ if ( point->flags2 & min_flag )
+ {
+ for ( nn = 0; nn < num_hints; nn++ )
+ {
+ PSH_Hint hint = sort[nn];
+ FT_Pos d = org_u - hint->org_pos;
+
+
+ if ( d < threshold && -d < threshold )
+ {
+ point->flags2 |= PSH_POINT_EDGE_MIN;
+ point->hint = hint;
+ psh_point_set_strong( point );
+ break;
+ }
+ }
+ }
+ else if ( point->flags2 & max_flag )
+ {
+ for ( nn = 0; nn < num_hints; nn++ )
+ {
+ PSH_Hint hint = sort[nn];
+ FT_Pos d = org_u - hint->org_pos - hint->org_len;
+
+
+ if ( d < threshold && -d < threshold )
+ {
+ point->flags2 |= PSH_POINT_EDGE_MAX;
+ point->hint = hint;
+ psh_point_set_strong( point );
+ break;
+ }
+ }
+ }
+
+ if ( point->hint == NULL )
+ {
+ for ( nn = 0; nn < num_hints; nn++ )
+ {
+ PSH_Hint hint = sort[nn];
+
+
+ if ( org_u >= hint->org_pos &&
+ org_u <= hint->org_pos + hint->org_len )
+ {
+ point->hint = hint;
+ break;
+ }
+ }
+ }
+ }
+
+#endif /* 1 */
+ }
+ }
+
+
+ /* the accepted shift for strong points in fractional pixels */
+#define PSH_STRONG_THRESHOLD 32
+
+ /* the maximum shift value in font units */
+#define PSH_STRONG_THRESHOLD_MAXIMUM 30
+
+
+ /* find strong points in a glyph */
+ static void
+ psh_glyph_find_strong_points( PSH_Glyph glyph,
+ FT_Int dimension )
+ {
+ /* a point is `strong' if it is located on a stem edge and */
+ /* has an `in' or `out' tangent parallel to the hint's direction */
+
+ PSH_Hint_Table table = &glyph->hint_tables[dimension];
+ PS_Mask mask = table->hint_masks->masks;
+ FT_UInt num_masks = table->hint_masks->num_masks;
+ FT_UInt first = 0;
+ FT_Int major_dir = dimension == 0 ? PSH_DIR_VERTICAL
+ : PSH_DIR_HORIZONTAL;
+ PSH_Dimension dim = &glyph->globals->dimension[dimension];
+ FT_Fixed scale = dim->scale_mult;
+ FT_Int threshold;
+
+
+ threshold = (FT_Int)FT_DivFix( PSH_STRONG_THRESHOLD, scale );
+ if ( threshold > PSH_STRONG_THRESHOLD_MAXIMUM )
+ threshold = PSH_STRONG_THRESHOLD_MAXIMUM;
+
+ /* process secondary hints to `selected' points */
+ if ( num_masks > 1 && glyph->num_points > 0 )
+ {
+ first = mask->end_point;
+ mask++;
+ for ( ; num_masks > 1; num_masks--, mask++ )
+ {
+ FT_UInt next;
+ FT_Int count;
+
+
+ next = mask->end_point;
+ count = next - first;
+ if ( count > 0 )
+ {
+ PSH_Point point = glyph->points + first;
+
+
+ psh_hint_table_activate_mask( table, mask );
+
+ psh_hint_table_find_strong_points( table, point, count,
+ threshold, major_dir );
+ }
+ first = next;
+ }
+ }
+
+ /* process primary hints for all points */
+ if ( num_masks == 1 )
+ {
+ FT_UInt count = glyph->num_points;
+ PSH_Point point = glyph->points;
+
+
+ psh_hint_table_activate_mask( table, table->hint_masks->masks );
+
+ psh_hint_table_find_strong_points( table, point, count,
+ threshold, major_dir );
+ }
+
+ /* now, certain points may have been attached to a hint and */
+ /* not marked as strong; update their flags then */
+ {
+ FT_UInt count = glyph->num_points;
+ PSH_Point point = glyph->points;
+
+
+ for ( ; count > 0; count--, point++ )
+ if ( point->hint && !psh_point_is_strong( point ) )
+ psh_point_set_strong( point );
+ }
+ }
+
+
+ /* find points in a glyph which are in a blue zone and have `in' or */
+ /* `out' tangents parallel to the horizontal axis */
+ static void
+ psh_glyph_find_blue_points( PSH_Blues blues,
+ PSH_Glyph glyph )
+ {
+ PSH_Blue_Table table;
+ PSH_Blue_Zone zone;
+ FT_UInt glyph_count = glyph->num_points;
+ FT_UInt blue_count;
+ PSH_Point point = glyph->points;
+
+
+ for ( ; glyph_count > 0; glyph_count--, point++ )
+ {
+ FT_Pos y;
+
+
+ /* check tangents */
+ if ( !PSH_DIR_COMPARE( point->dir_in, PSH_DIR_HORIZONTAL ) &&
+ !PSH_DIR_COMPARE( point->dir_out, PSH_DIR_HORIZONTAL ) )
+ continue;
+
+ /* skip strong points */
+ if ( psh_point_is_strong( point ) )
+ continue;
+
+ y = point->org_u;
+
+ /* look up top zones */
+ table = &blues->normal_top;
+ blue_count = table->count;
+ zone = table->zones;
+
+ for ( ; blue_count > 0; blue_count--, zone++ )
+ {
+ FT_Pos delta = y - zone->org_bottom;
+
+
+ if ( delta < -blues->blue_fuzz )
+ break;
+
+ if ( y <= zone->org_top + blues->blue_fuzz )
+ if ( blues->no_overshoots || delta <= blues->blue_threshold )
+ {
+ point->cur_u = zone->cur_bottom;
+ psh_point_set_strong( point );
+ psh_point_set_fitted( point );
+ }
+ }
+
+ /* look up bottom zones */
+ table = &blues->normal_bottom;
+ blue_count = table->count;
+ zone = table->zones + blue_count - 1;
+
+ for ( ; blue_count > 0; blue_count--, zone-- )
+ {
+ FT_Pos delta = zone->org_top - y;
+
+
+ if ( delta < -blues->blue_fuzz )
+ break;
+
+ if ( y >= zone->org_bottom - blues->blue_fuzz )
+ if ( blues->no_overshoots || delta < blues->blue_threshold )
+ {
+ point->cur_u = zone->cur_top;
+ psh_point_set_strong( point );
+ psh_point_set_fitted( point );
+ }
+ }
+ }
+ }
+
+
+ /* interpolate strong points with the help of hinted coordinates */
+ static void
+ psh_glyph_interpolate_strong_points( PSH_Glyph glyph,
+ FT_Int dimension )
+ {
+ PSH_Dimension dim = &glyph->globals->dimension[dimension];
+ FT_Fixed scale = dim->scale_mult;
+
+ FT_UInt count = glyph->num_points;
+ PSH_Point point = glyph->points;
+
+
+ for ( ; count > 0; count--, point++ )
+ {
+ PSH_Hint hint = point->hint;
+
+
+ if ( hint )
+ {
+ FT_Pos delta;
+
+
+ if ( psh_point_is_edge_min( point ) )
+ point->cur_u = hint->cur_pos;
+
+ else if ( psh_point_is_edge_max( point ) )
+ point->cur_u = hint->cur_pos + hint->cur_len;
+
+ else
+ {
+ delta = point->org_u - hint->org_pos;
+
+ if ( delta <= 0 )
+ point->cur_u = hint->cur_pos + FT_MulFix( delta, scale );
+
+ else if ( delta >= hint->org_len )
+ point->cur_u = hint->cur_pos + hint->cur_len +
+ FT_MulFix( delta - hint->org_len, scale );
+
+ else if ( hint->org_len > 0 )
+ point->cur_u = hint->cur_pos +
+ FT_MulDiv( delta, hint->cur_len,
+ hint->org_len );
+ else
+ point->cur_u = hint->cur_pos;
+ }
+ psh_point_set_fitted( point );
+ }
+ }
+ }
+
+
+#define PSH_MAX_STRONG_INTERNAL 16
+
+ static void
+ psh_glyph_interpolate_normal_points( PSH_Glyph glyph,
+ FT_Int dimension )
+ {
+
+#if 1
+ /* first technique: a point is strong if it is a local extremum */
+
+ PSH_Dimension dim = &glyph->globals->dimension[dimension];
+ FT_Fixed scale = dim->scale_mult;
+ FT_Memory memory = glyph->memory;
+
+ PSH_Point* strongs = NULL;
+ PSH_Point strongs_0[PSH_MAX_STRONG_INTERNAL];
+ FT_UInt num_strongs = 0;
+
+ PSH_Point points = glyph->points;
+ PSH_Point points_end = points + glyph->num_points;
+ PSH_Point point;
+
+
+ /* first count the number of strong points */
+ for ( point = points; point < points_end; point++ )
+ {
+ if ( psh_point_is_strong( point ) )
+ num_strongs++;
+ }
+
+ if ( num_strongs == 0 ) /* nothing to do here */
+ return;
+
+ /* allocate an array to store a list of points, */
+ /* stored in increasing org_u order */
+ if ( num_strongs <= PSH_MAX_STRONG_INTERNAL )
+ strongs = strongs_0;
+ else
+ {
+ FT_Error error;
+
+
+ if ( FT_NEW_ARRAY( strongs, num_strongs ) )
+ return;
+ }
+
+ num_strongs = 0;
+ for ( point = points; point < points_end; point++ )
+ {
+ PSH_Point* insert;
+
+
+ if ( !psh_point_is_strong( point ) )
+ continue;
+
+ for ( insert = strongs + num_strongs; insert > strongs; insert-- )
+ {
+ if ( insert[-1]->org_u <= point->org_u )
+ break;
+
+ insert[0] = insert[-1];
+ }
+ insert[0] = point;
+ num_strongs++;
+ }
+
+ /* now try to interpolate all normal points */
+ for ( point = points; point < points_end; point++ )
+ {
+ if ( psh_point_is_strong( point ) )
+ continue;
+
+ /* sometimes, some local extrema are smooth points */
+ if ( psh_point_is_smooth( point ) )
+ {
+ if ( point->dir_in == PSH_DIR_NONE ||
+ point->dir_in != point->dir_out )
+ continue;
+
+ if ( !psh_point_is_extremum( point ) &&
+ !psh_point_is_inflex( point ) )
+ continue;
+
+ point->flags &= ~PSH_POINT_SMOOTH;
+ }
+
+ /* find best enclosing point coordinates then interpolate */
+ {
+ PSH_Point before, after;
+ FT_UInt nn;
+
+
+ for ( nn = 0; nn < num_strongs; nn++ )
+ if ( strongs[nn]->org_u > point->org_u )
+ break;
+
+ if ( nn == 0 ) /* point before the first strong point */
+ {
+ after = strongs[0];
+
+ point->cur_u = after->cur_u +
+ FT_MulFix( point->org_u - after->org_u,
+ scale );
+ }
+ else
+ {
+ before = strongs[nn - 1];
+
+ for ( nn = num_strongs; nn > 0; nn-- )
+ if ( strongs[nn - 1]->org_u < point->org_u )
+ break;
+
+ if ( nn == num_strongs ) /* point is after last strong point */
+ {
+ before = strongs[nn - 1];
+
+ point->cur_u = before->cur_u +
+ FT_MulFix( point->org_u - before->org_u,
+ scale );
+ }
+ else
+ {
+ FT_Pos u;
+
+
+ after = strongs[nn];
+
+ /* now interpolate point between before and after */
+ u = point->org_u;
+
+ if ( u == before->org_u )
+ point->cur_u = before->cur_u;
+
+ else if ( u == after->org_u )
+ point->cur_u = after->cur_u;
+
+ else
+ point->cur_u = before->cur_u +
+ FT_MulDiv( u - before->org_u,
+ after->cur_u - before->cur_u,
+ after->org_u - before->org_u );
+ }
+ }
+ psh_point_set_fitted( point );
+ }
+ }
+
+ if ( strongs != strongs_0 )
+ FT_FREE( strongs );
+
+#endif /* 1 */
+
+ }
+
+
+ /* interpolate other points */
+ static void
+ psh_glyph_interpolate_other_points( PSH_Glyph glyph,
+ FT_Int dimension )
+ {
+ PSH_Dimension dim = &glyph->globals->dimension[dimension];
+ FT_Fixed scale = dim->scale_mult;
+ FT_Fixed delta = dim->scale_delta;
+ PSH_Contour contour = glyph->contours;
+ FT_UInt num_contours = glyph->num_contours;
+
+
+ for ( ; num_contours > 0; num_contours--, contour++ )
+ {
+ PSH_Point start = contour->start;
+ PSH_Point first, next, point;
+ FT_UInt fit_count;
+
+
+ /* count the number of strong points in this contour */
+ next = start + contour->count;
+ fit_count = 0;
+ first = 0;
+
+ for ( point = start; point < next; point++ )
+ if ( psh_point_is_fitted( point ) )
+ {
+ if ( !first )
+ first = point;
+
+ fit_count++;
+ }
+
+ /* if there are less than 2 fitted points in the contour, we */
+ /* simply scale and eventually translate the contour points */
+ if ( fit_count < 2 )
+ {
+ if ( fit_count == 1 )
+ delta = first->cur_u - FT_MulFix( first->org_u, scale );
+
+ for ( point = start; point < next; point++ )
+ if ( point != first )
+ point->cur_u = FT_MulFix( point->org_u, scale ) + delta;
+
+ goto Next_Contour;
+ }
+
+ /* there are more than 2 strong points in this contour; we */
+ /* need to interpolate weak points between them */
+ start = first;
+ do
+ {
+ point = first;
+
+ /* skip consecutive fitted points */
+ for (;;)
+ {
+ next = first->next;
+ if ( next == start )
+ goto Next_Contour;
+
+ if ( !psh_point_is_fitted( next ) )
+ break;
+
+ first = next;
+ }
+
+ /* find next fitted point after unfitted one */
+ for (;;)
+ {
+ next = next->next;
+ if ( psh_point_is_fitted( next ) )
+ break;
+ }
+
+ /* now interpolate between them */
+ {
+ FT_Pos org_a, org_ab, cur_a, cur_ab;
+ FT_Pos org_c, org_ac, cur_c;
+ FT_Fixed scale_ab;
+
+
+ if ( first->org_u <= next->org_u )
+ {
+ org_a = first->org_u;
+ cur_a = first->cur_u;
+ org_ab = next->org_u - org_a;
+ cur_ab = next->cur_u - cur_a;
+ }
+ else
+ {
+ org_a = next->org_u;
+ cur_a = next->cur_u;
+ org_ab = first->org_u - org_a;
+ cur_ab = first->cur_u - cur_a;
+ }
+
+ scale_ab = 0x10000L;
+ if ( org_ab > 0 )
+ scale_ab = FT_DivFix( cur_ab, org_ab );
+
+ point = first->next;
+ do
+ {
+ org_c = point->org_u;
+ org_ac = org_c - org_a;
+
+ if ( org_ac <= 0 )
+ {
+ /* on the left of the interpolation zone */
+ cur_c = cur_a + FT_MulFix( org_ac, scale );
+ }
+ else if ( org_ac >= org_ab )
+ {
+ /* on the right on the interpolation zone */
+ cur_c = cur_a + cur_ab + FT_MulFix( org_ac - org_ab, scale );
+ }
+ else
+ {
+ /* within the interpolation zone */
+ cur_c = cur_a + FT_MulFix( org_ac, scale_ab );
+ }
+
+ point->cur_u = cur_c;
+
+ point = point->next;
+
+ } while ( point != next );
+ }
+
+ /* keep going until all points in the contours have been processed */
+ first = next;
+
+ } while ( first != start );
+
+ Next_Contour:
+ ;
+ }
+ }
+
+
+ /*************************************************************************/
+ /*************************************************************************/
+ /***** *****/
+ /***** HIGH-LEVEL INTERFACE *****/
+ /***** *****/
+ /*************************************************************************/
+ /*************************************************************************/
+
+ FT_Error
+ ps_hints_apply( PS_Hints ps_hints,
+ FT_Outline* outline,
+ PSH_Globals globals,
+ FT_Render_Mode hint_mode )
+ {
+ PSH_GlyphRec glyphrec;
+ PSH_Glyph glyph = &glyphrec;
+ FT_Error error;
+#ifdef DEBUG_HINTER
+ FT_Memory memory;
+#endif
+ FT_Int dimension;
+
+
+ /* something to do? */
+ if ( outline->n_points == 0 || outline->n_contours == 0 )
+ return PSH_Err_Ok;
+
+#ifdef DEBUG_HINTER
+
+ memory = globals->memory;
+
+ if ( ps_debug_glyph )
+ {
+ psh_glyph_done( ps_debug_glyph );
+ FT_FREE( ps_debug_glyph );
+ }
+
+ if ( FT_NEW( glyph ) )
+ return error;
+
+ ps_debug_glyph = glyph;
+
+#endif /* DEBUG_HINTER */
+
+ error = psh_glyph_init( glyph, outline, ps_hints, globals );
+ if ( error )
+ goto Exit;
+
+ /* try to optimize the y_scale so that the top of non-capital letters
+ * is aligned on a pixel boundary whenever possible
+ */
+ {
+ PSH_Dimension dim_x = &glyph->globals->dimension[0];
+ PSH_Dimension dim_y = &glyph->globals->dimension[1];
+
+ FT_Fixed x_scale = dim_x->scale_mult;
+ FT_Fixed y_scale = dim_y->scale_mult;
+
+ FT_Fixed scaled;
+ FT_Fixed fitted;
+
+
+ scaled = FT_MulFix( globals->blues.normal_top.zones->org_ref, y_scale );
+ fitted = FT_PIX_ROUND( scaled );
+
+ if ( fitted != 0 && scaled != fitted )
+ {
+ y_scale = FT_MulDiv( y_scale, fitted, scaled );
+
+ if ( fitted < scaled )
+ x_scale -= x_scale / 50;
+
+ psh_globals_set_scale( glyph->globals, x_scale, y_scale, 0, 0 );
+ }
+ }
+
+ glyph->do_horz_hints = 1;
+ glyph->do_vert_hints = 1;
+
+ glyph->do_horz_snapping = FT_BOOL( hint_mode == FT_RENDER_MODE_MONO ||
+ hint_mode == FT_RENDER_MODE_LCD );
+
+ glyph->do_vert_snapping = FT_BOOL( hint_mode == FT_RENDER_MODE_MONO ||
+ hint_mode == FT_RENDER_MODE_LCD_V );
+
+ glyph->do_stem_adjust = FT_BOOL( hint_mode != FT_RENDER_MODE_LIGHT );
+
+ for ( dimension = 0; dimension < 2; dimension++ )
+ {
+ /* load outline coordinates into glyph */
+ psh_glyph_load_points( glyph, dimension );
+
+ /* compute local extrema */
+ psh_glyph_compute_extrema( glyph );
+
+ /* compute aligned stem/hints positions */
+ psh_hint_table_align_hints( &glyph->hint_tables[dimension],
+ glyph->globals,
+ dimension,
+ glyph );
+
+ /* find strong points, align them, then interpolate others */
+ psh_glyph_find_strong_points( glyph, dimension );
+ if ( dimension == 1 )
+ psh_glyph_find_blue_points( &globals->blues, glyph );
+ psh_glyph_interpolate_strong_points( glyph, dimension );
+ psh_glyph_interpolate_normal_points( glyph, dimension );
+ psh_glyph_interpolate_other_points( glyph, dimension );
+
+ /* save hinted coordinates back to outline */
+ psh_glyph_save_points( glyph, dimension );
+ }
+
+ Exit:
+
+#ifndef DEBUG_HINTER
+ psh_glyph_done( glyph );
+#endif
+
+ return error;
+ }
+
+
+/* END */