30#define FT_COMPONENT pshrec
33 PS_Hints ps_debug_hints = NULL;
34 int ps_debug_no_horz_hints = 0;
35 int ps_debug_no_vert_hints = 0;
49 ps_hint_table_done( PS_Hint_Table table,
52 FT_FREE( table->hints );
60 ps_hint_table_ensure( PS_Hint_Table table,
64 FT_UInt old_max = table->max_hints;
65 FT_UInt new_max = count;
70 new_max = FT_PAD_CEIL( new_max, 8 );
71 if ( !FT_QRENEW_ARRAY( table->hints, old_max, new_max ) )
72 table->max_hints = new_max;
79 ps_hint_table_alloc( PS_Hint_Table table,
83 FT_Error error = FT_Err_Ok;
88 count = table->num_hints;
91 if ( count > table->max_hints )
93 error = ps_hint_table_ensure( table, count, memory );
98 hint = table->hints + count - 1;
100 table->num_hints = count;
118 ps_mask_done( PS_Mask mask,
121 FT_FREE( mask->bytes );
130 ps_mask_ensure( PS_Mask mask,
134 FT_UInt old_max = mask->max_bits >> 3;
135 FT_UInt new_max = ( count + 7 ) >> 3;
136 FT_Error error = FT_Err_Ok;
139 if ( new_max > old_max )
141 new_max = FT_PAD_CEIL( new_max, 8 );
143 if ( !FT_RENEW_ARRAY( mask->bytes, old_max, new_max ) )
144 mask->max_bits = new_max * 8;
152 ps_mask_test_bit( PS_Mask mask,
155 if ( idx >= mask->num_bits )
158 return mask->bytes[idx >> 3] & ( 0x80 >> ( idx & 7 ) );
164 ps_mask_set_bit( PS_Mask mask,
168 FT_Error error = FT_Err_Ok;
172 if ( idx >= mask->num_bits )
174 error = ps_mask_ensure( mask, idx + 1, memory );
178 mask->num_bits = idx + 1;
181 p = mask->bytes + ( idx >> 3 );
182 p[0] = (FT_Byte)( p[0] | ( 0x80 >> ( idx & 7 ) ) );
191 ps_mask_table_done( PS_Mask_Table table,
194 FT_UInt count = table->max_masks;
195 PS_Mask mask = table->masks;
198 for ( ; count > 0; count--, mask++ )
199 ps_mask_done( mask, memory );
201 FT_FREE( table->masks );
202 table->num_masks = 0;
203 table->max_masks = 0;
209 ps_mask_table_ensure( PS_Mask_Table table,
213 FT_UInt old_max = table->max_masks;
214 FT_UInt new_max = count;
215 FT_Error error = FT_Err_Ok;
218 if ( new_max > old_max )
220 new_max = FT_PAD_CEIL( new_max, 8 );
221 if ( !FT_RENEW_ARRAY( table->masks, old_max, new_max ) )
222 table->max_masks = new_max;
230 ps_mask_table_alloc( PS_Mask_Table table,
235 FT_Error error = FT_Err_Ok;
239 count = table->num_masks;
242 if ( count > table->max_masks )
244 error = ps_mask_table_ensure( table, count, memory );
249 mask = table->masks + count - 1;
253 if ( mask->max_bits )
254 FT_MEM_ZERO( mask->bytes, mask->max_bits >> 3 );
256 table->num_masks = count;
266 ps_mask_table_last( PS_Mask_Table table,
270 FT_Error error = FT_Err_Ok;
275 count = table->num_masks;
278 error = ps_mask_table_alloc( table, memory, &mask );
283 mask = table->masks + count - 1;
293 ps_mask_table_set_bits( PS_Mask_Table table,
294 const FT_Byte* source,
303 error = ps_mask_table_last( table, memory, &mask );
307 error = ps_mask_ensure( mask, bit_count, memory );
311 mask->num_bits = bit_count;
315 FT_Byte* read = (FT_Byte*)source + ( bit_pos >> 3 );
316 FT_Int rmask = 0x80 >> ( bit_pos & 7 );
317 FT_Byte* write = mask->bytes;
322 for ( ; bit_count > 0; bit_count-- )
324 val = write[0] & ~wmask;
326 if ( read[0] & rmask )
329 write[0] = (FT_Byte)val;
354 ps_mask_table_test_intersect( PS_Mask_Table table,
358 PS_Mask mask1 = table->masks + index1;
359 PS_Mask mask2 = table->masks + index2;
360 FT_Byte* p1 = mask1->bytes;
361 FT_Byte* p2 = mask2->bytes;
362 FT_UInt count1 = mask1->num_bits;
363 FT_UInt count2 = mask2->num_bits;
367 count = FT_MIN( count1, count2 );
368 for ( ; count >= 8; count -= 8 )
380 return ( p1[0] & p2[0] ) & ~( 0xFF >> count );
386 ps_mask_table_merge( PS_Mask_Table table,
391 FT_Error error = FT_Err_Ok;
395 if ( index1 > index2 )
405 if ( index1 < index2 && index2 < table->num_masks )
409 PS_Mask mask1 = table->masks + index1;
410 PS_Mask mask2 = table->masks + index2;
411 FT_UInt count1 = mask1->num_bits;
412 FT_UInt count2 = mask2->num_bits;
425 if ( count2 > count1 )
427 error = ps_mask_ensure( mask1, count2, memory );
431 mask1->num_bits = count2;
436 write = mask1->bytes;
437 pos = ( count2 + 7 ) >> 3;
439 for ( ; pos > 0; pos-- )
441 write[0] = (FT_Byte)( write[0] | read[0] );
450 mask2->end_point = 0;
453 delta = table->num_masks - 1 - index2;
457 PS_MaskRec dummy = *mask2;
462 delta *
sizeof ( PS_MaskRec ) );
464 mask2[delta] = dummy;
470 FT_TRACE0((
"ps_mask_table_merge: ignoring invalid indices (%u,%u)\n",
482 ps_mask_table_merge_all( PS_Mask_Table table,
485 FT_UInt index1, index2;
486 FT_Error error = FT_Err_Ok;
490 for ( index1 = table->num_masks - 1; index1 < table->num_masks; index1-- )
492 for ( index2 = index1 - 1; index2 < index1; index2-- )
494 if ( ps_mask_table_test_intersect( table, index1, index2 ) )
496 error = ps_mask_table_merge( table, index2, index1, memory );
521 ps_dimension_done( PS_Dimension dimension,
524 ps_mask_table_done( &dimension->counters, memory );
525 ps_mask_table_done( &dimension->masks, memory );
526 ps_hint_table_done( &dimension->hints, memory );
532 ps_dimension_init( PS_Dimension dimension )
534 dimension->hints.num_hints = 0;
535 dimension->masks.num_masks = 0;
536 dimension->counters.num_masks = 0;
544 ps_dimension_set_mask_bit( PS_Dimension dim,
549 FT_Error error = FT_Err_Ok;
553 error = ps_mask_table_last( &dim->masks, memory, &mask );
557 error = ps_mask_set_bit( mask, idx, memory );
567 ps_dimension_end_mask( PS_Dimension dim,
570 FT_UInt count = dim->masks.num_masks;
575 PS_Mask mask = dim->masks.masks + count - 1;
578 mask->end_point = end_point;
586 ps_dimension_reset_mask( PS_Dimension dim,
594 ps_dimension_end_mask( dim, end_point );
597 return ps_mask_table_alloc( &dim->masks, memory, &mask );
603 ps_dimension_set_mask_bits( PS_Dimension dim,
604 const FT_Byte* source,
614 error = ps_dimension_reset_mask( dim, end_point, memory );
619 error = ps_mask_table_set_bits( &dim->masks, source,
620 source_pos, source_bits, memory );
629 ps_dimension_add_t1stem( PS_Dimension dim,
635 FT_Error error = FT_Err_Ok;
642 flags |= PS_HINT_FLAG_GHOST;
645 flags |= PS_HINT_FLAG_BOTTOM;
646 pos = ADD_INT( pos, len );
655 FT_UInt max = dim->hints.num_hints;
656 PS_Hint hint = dim->hints.hints;
659 for ( idx = 0; idx < max; idx++, hint++ )
661 if ( hint->pos == pos && hint->len == len )
668 error = ps_hint_table_alloc( &dim->hints, memory, &hint );
678 error = ps_mask_table_last( &dim->masks, memory, &mask );
682 error = ps_mask_set_bit( mask, idx, memory );
697 ps_dimension_add_counter( PS_Dimension dim,
703 FT_Error error = FT_Err_Ok;
704 FT_UInt count = dim->counters.num_masks;
705 PS_Mask counter = dim->counters.masks;
710 for ( ; count > 0; count--, counter++ )
712 if ( ps_mask_test_bit( counter, hint1 ) ||
713 ps_mask_test_bit( counter, hint2 ) ||
714 ps_mask_test_bit( counter, hint3 ) )
721 error = ps_mask_table_alloc( &dim->counters, memory, &counter );
727 error = ps_mask_set_bit( counter, hint1, memory );
731 error = ps_mask_set_bit( counter, hint2, memory );
735 error = ps_mask_set_bit( counter, hint3, memory );
746 ps_dimension_end( PS_Dimension dim,
751 ps_dimension_end_mask( dim, end_point );
754 return ps_mask_table_merge_all( &dim->counters, memory );
769 ps_hints_done( PS_Hints hints )
771 FT_Memory memory = hints->memory;
774 ps_dimension_done( &hints->dimension[0], memory );
775 ps_dimension_done( &hints->dimension[1], memory );
777 hints->error = FT_Err_Ok;
778 hints->memory = NULL;
783 ps_hints_init( PS_Hints hints,
787 hints->memory = memory;
793 ps_hints_open( PS_Hints hints,
794 PS_Hint_Type hint_type )
796 hints->error = FT_Err_Ok;
797 hints->hint_type = hint_type;
799 ps_dimension_init( &hints->dimension[0] );
800 ps_dimension_init( &hints->dimension[1] );
806 ps_hints_stem( PS_Hints hints,
820 FT_TRACE0((
"ps_hints_stem: invalid dimension (%u) used\n",
822 dimension = ( dimension != 0 );
827 dim = &hints->dimension[dimension];
829 for ( ; count > 0; count--, stems += 2 )
832 FT_Memory memory = hints->memory;
835 error = ps_dimension_add_t1stem( dim,
842 FT_ERROR((
"ps_hints_stem: could not add stem"
843 " (%ld,%ld) to hints table\n", stems[0], stems[1] ));
845 hints->error = error;
854 ps_hints_t1stem3( T1_Hints hints_,
858 PS_Hints hints = (PS_Hints)hints_;
859 FT_Error error = FT_Err_Ok;
865 FT_Memory memory = hints->memory;
873 FT_TRACE0((
"ps_hints_t1stem3: invalid dimension (%u) used\n",
875 dimension = ( dimension != 0 );
878 dim = &hints->dimension[dimension];
881 if ( hints->hint_type == PS_HINT_TYPE_1 )
884 for ( count = 0; count < 3; count++, stems += 2 )
886 error = ps_dimension_add_t1stem( dim,
887 (FT_Int)FIXED_TO_INT( stems[0] ),
888 (FT_Int)FIXED_TO_INT( stems[1] ),
889 memory, &idx[count] );
895 error = ps_dimension_add_counter( dim, idx[0], idx[1], idx[2],
902 FT_ERROR((
"ps_hints_t1stem3: called with invalid hint type\n" ));
903 error = FT_THROW( Invalid_Argument );
911 FT_ERROR((
"ps_hints_t1stem3: could not add counter stems to table\n" ));
912 hints->error = error;
918 ps_hints_t1reset( T1_Hints hints_,
921 PS_Hints hints = (PS_Hints)hints_;
922 FT_Error error = FT_Err_Ok;
927 FT_Memory memory = hints->memory;
930 if ( hints->hint_type == PS_HINT_TYPE_1 )
932 error = ps_dimension_reset_mask( &hints->dimension[0],
937 error = ps_dimension_reset_mask( &hints->dimension[1],
945 error = FT_THROW( Invalid_Argument );
952 hints->error = error;
958 ps_hints_t2mask( T2_Hints hints_,
961 const FT_Byte* bytes )
963 PS_Hints hints = (PS_Hints)hints_;
969 PS_Dimension dim = hints->dimension;
970 FT_Memory memory = hints->memory;
971 FT_UInt count1 = dim[0].hints.num_hints;
972 FT_UInt count2 = dim[1].hints.num_hints;
976 if ( bit_count != count1 + count2 )
978 FT_TRACE0((
"ps_hints_t2mask:"
979 " called with invalid bitcount %u (instead of %u)\n",
980 bit_count, count1 + count2 ));
987 error = ps_dimension_set_mask_bits( &dim[0], bytes, count2, count1,
992 error = ps_dimension_set_mask_bits( &dim[1], bytes, 0, count2,
1000 hints->error = error;
1005 ps_hints_t2counter( T2_Hints hints_,
1007 const FT_Byte* bytes )
1009 PS_Hints hints = (PS_Hints)hints_;
1013 if ( !hints->error )
1015 PS_Dimension dim = hints->dimension;
1016 FT_Memory memory = hints->memory;
1017 FT_UInt count1 = dim[0].hints.num_hints;
1018 FT_UInt count2 = dim[1].hints.num_hints;
1022 if ( bit_count != count1 + count2 )
1024 FT_TRACE0((
"ps_hints_t2counter:"
1025 " called with invalid bitcount %u (instead of %u)\n",
1026 bit_count, count1 + count2 ));
1033 error = ps_dimension_set_mask_bits( &dim[0], bytes, 0, count1,
1038 error = ps_dimension_set_mask_bits( &dim[1], bytes, count1, count2,
1046 hints->error = error;
1052 ps_hints_close( PS_Hints hints,
1058 error = hints->error;
1061 FT_Memory memory = hints->memory;
1062 PS_Dimension dim = hints->dimension;
1065 error = ps_dimension_end( &dim[0], end_point, memory );
1068 error = ps_dimension_end( &dim[1], end_point, memory );
1074 ps_debug_hints = hints;
1089 t1_hints_open( T1_Hints hints )
1091 ps_hints_open( (PS_Hints)hints, PS_HINT_TYPE_1 );
1095 t1_hints_close( T1_Hints hints,
1098 return ps_hints_close( (PS_Hints)hints, end_point );
1102 t1_hints_stem( T1_Hints hints,
1109 stems[0] = FIXED_TO_INT( coords[0] );
1110 stems[1] = FIXED_TO_INT( coords[1] );
1112 ps_hints_stem( (PS_Hints)hints, dimension, 1, stems );
1117 t1_hints_apply( T1_Hints hints,
1118 FT_Outline* outline,
1119 PSH_Globals globals,
1120 FT_Render_Mode hint_mode )
1122 return ps_hints_apply( (PS_Hints)hints, outline, globals, hint_mode );
1126 FT_LOCAL_DEF(
void )
1127 t1_hints_funcs_init( T1_Hints_FuncsRec* funcs )
1131 funcs->open = (T1_Hints_OpenFunc) t1_hints_open;
1132 funcs->close = (T1_Hints_CloseFunc) t1_hints_close;
1133 funcs->stem = (T1_Hints_SetStemFunc) t1_hints_stem;
1134 funcs->stem3 = (T1_Hints_SetStem3Func)ps_hints_t1stem3;
1135 funcs->reset = (T1_Hints_ResetFunc) ps_hints_t1reset;
1136 funcs->apply = (T1_Hints_ApplyFunc) t1_hints_apply;
1149 t2_hints_open( T2_Hints hints )
1151 ps_hints_open( (PS_Hints)hints, PS_HINT_TYPE_2 );
1156 t2_hints_close( T2_Hints hints,
1159 return ps_hints_close( (PS_Hints)hints, end_point );
1164 t2_hints_stems( T2_Hints hints,
1169 FT_Pos stems[32], y;
1170 FT_Int total = count, n;
1182 for ( n = 0; n < count * 2; n++ )
1184 y = ADD_LONG( y, coords[n] );
1185 stems[n] = FIXED_TO_INT( y );
1189 for ( n = 0; n < count * 2; n += 2 )
1190 stems[n + 1] = stems[n + 1] - stems[n];
1193 ps_hints_stem( (PS_Hints)hints, dimension, count, stems );
1201 t2_hints_apply( T2_Hints hints,
1202 FT_Outline* outline,
1203 PSH_Globals globals,
1204 FT_Render_Mode hint_mode )
1206 return ps_hints_apply( (PS_Hints)hints, outline, globals, hint_mode );
1210 FT_LOCAL_DEF(
void )
1211 t2_hints_funcs_init( T2_Hints_FuncsRec* funcs )
1215 funcs->open = (T2_Hints_OpenFunc) t2_hints_open;
1216 funcs->close = (T2_Hints_CloseFunc) t2_hints_close;
1217 funcs->stems = (T2_Hints_StemsFunc) t2_hints_stems;
1218 funcs->hintmask = (T2_Hints_MaskFunc) ps_hints_t2mask;
1219 funcs->counter = (T2_Hints_CounterFunc)ps_hints_t2counter;
1220 funcs->apply = (T2_Hints_ApplyFunc) t2_hints_apply;