1 /****************************************************************************
5 * FreeType PostScript hints recorder (body).
7 * Copyright (C) 2001-2023 by
8 * David Turner, Robert Wilhelm, and Werner Lemberg.
10 * This file is part of the FreeType project, and may only be used,
11 * modified, and distributed under the terms of the FreeType project
12 * license, LICENSE.TXT. By continuing to use, modify, or distribute
13 * this file you indicate that you have read the license and
14 * understand and accept it fully.
19 #include <freetype/freetype.h>
20 #include <freetype/internal/ftobjs.h>
21 #include <freetype/internal/ftdebug.h>
22 #include <freetype/internal/ftcalc.h>
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;
39 /*************************************************************************/
40 /*************************************************************************/
42 /***** PS_HINT MANAGEMENT *****/
44 /*************************************************************************/
45 /*************************************************************************/
47 /* destroy hints table */
49 ps_hint_table_done( PS_Hint_Table table,
52 FT_FREE( table->hints );
58 /* ensure that a table can contain "count" elements */
60 ps_hint_table_ensure( PS_Hint_Table table,
64 FT_UInt old_max = table->max_hints;
65 FT_UInt new_max = count;
69 /* try to grow the table */
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; /* initialized upstream */
100 table->num_hints = count;
108 /*************************************************************************/
109 /*************************************************************************/
111 /***** PS_MASK MANAGEMENT *****/
113 /*************************************************************************/
114 /*************************************************************************/
118 ps_mask_done( PS_Mask mask,
121 FT_FREE( mask->bytes );
128 /* ensure that a mask can contain "count" bits */
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 );
142 /* added bytes are zeroed here */
143 if ( !FT_RENEW_ARRAY( mask->bytes, old_max, new_max ) )
144 mask->max_bits = new_max * 8;
150 /* test a bit value in a given mask */
152 ps_mask_test_bit( PS_Mask mask,
155 if ( idx >= mask->num_bits )
158 return mask->bytes[idx >> 3] & ( 0x80 >> ( idx & 7 ) );
162 /* set a given bit, possibly grow the mask */
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 ) ) );
189 /* destroy mask table */
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;
207 /* ensure that a mask table can contain "count" masks */
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;
228 /* allocate a new mask in a table */
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;
252 /* reused mask must be cleared */
253 if ( mask->max_bits )
254 FT_MEM_ZERO( mask->bytes, mask->max_bits >> 3 );
256 table->num_masks = count;
264 /* return last hint mask in a table, create one if the table is empty */
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;
291 /* set a new mask to a given bit range */
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;
352 /* test whether two masks in a table intersect */
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 );
384 /* merge two masks, used by ps_mask_table_merge_all */
386 ps_mask_table_merge( PS_Mask_Table table,
391 FT_Error error = FT_Err_Ok;
394 /* swap index1 and index2 so that index1 < index2 */
395 if ( index1 > index2 )
405 if ( index1 < index2 && index2 < table->num_masks )
407 /* we need to merge the bitsets of index1 and index2 with a */
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;
423 /* if "count2" is greater than "count1", we need to grow the */
425 if ( count2 > count1 )
427 error = ps_mask_ensure( mask1, count2, memory );
431 mask1->num_bits = count2;
434 /* merge (unite) the bitsets */
436 write = mask1->bytes;
437 pos = ( count2 + 7 ) >> 3;
439 for ( ; pos > 0; pos-- )
441 write[0] = (FT_Byte)( write[0] | read[0] );
447 /* Now, remove "mask2" from the list. We need to keep the masks */
448 /* sorted in order of importance, so move table elements. */
450 mask2->end_point = 0;
452 /* number of masks to move */
453 delta = table->num_masks - 1 - index2;
456 /* move to end of table for reuse */
457 PS_MaskRec dummy = *mask2;
462 delta * sizeof ( PS_MaskRec ) );
464 mask2[delta] = dummy;
470 FT_TRACE0(( "ps_mask_table_merge: ignoring invalid indices (%d,%d)\n",
478 /* Try to merge all masks in a given table. This is used to merge */
479 /* all counter masks into independent counter "paths". */
482 ps_mask_table_merge_all( PS_Mask_Table table,
485 FT_UInt index1, index2;
486 FT_Error error = FT_Err_Ok;
489 /* the loops stop when unsigned indices wrap around after 0 */
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 );
510 /*************************************************************************/
511 /*************************************************************************/
513 /***** PS_DIMENSION MANAGEMENT *****/
515 /*************************************************************************/
516 /*************************************************************************/
519 /* finalize a given dimension */
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 );
530 /* initialize a given dimension */
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;
542 /* set a bit at a given index in the current hint mask */
544 ps_dimension_set_mask_bit( PS_Dimension dim,
549 FT_Error error = FT_Err_Ok;
552 /* get last hint mask */
553 error = ps_mask_table_last( &dim->masks, memory, &mask );
557 error = ps_mask_set_bit( mask, idx, memory );
565 /* set the end point in a mask, called from "End" & "Reset" methods */
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;
583 /* set the end point in the current mask, then create a new empty one */
584 /* (called by "Reset" method) */
586 ps_dimension_reset_mask( PS_Dimension dim,
593 /* end current mask */
594 ps_dimension_end_mask( dim, end_point );
596 /* allocate new one */
597 return ps_mask_table_alloc( &dim->masks, memory, &mask );
601 /* set a new mask, called from the "T2Stem" method */
603 ps_dimension_set_mask_bits( PS_Dimension dim,
604 const FT_Byte* source,
613 /* reset current mask, if any */
614 error = ps_dimension_reset_mask( dim, end_point, memory );
618 /* set bits in new mask */
619 error = ps_mask_table_set_bits( &dim->masks, source,
620 source_pos, source_bits, memory );
627 /* add a new single stem (called from "T1Stem" method) */
629 ps_dimension_add_t1stem( PS_Dimension dim,
635 FT_Error error = FT_Err_Ok;
639 /* detect ghost stem */
642 flags |= PS_HINT_FLAG_GHOST;
645 flags |= PS_HINT_FLAG_BOTTOM;
646 pos = ADD_INT( pos, len );
651 /* now, lookup stem in the current hints table */
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 )
665 /* we need to create a new hint in the table */
668 error = ps_hint_table_alloc( &dim->hints, memory, &hint );
677 /* now, store the hint in the current mask */
678 error = ps_mask_table_last( &dim->masks, memory, &mask );
682 error = ps_mask_set_bit( mask, idx, memory );
695 /* add a "hstem3/vstem3" counter to our dimension table */
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;
708 /* try to find an existing counter mask that already uses */
709 /* one of these stems here */
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 ) )
718 /* create a new counter when needed */
721 error = ps_mask_table_alloc( &dim->counters, memory, &counter );
726 /* now, set the bits for our hints in the counter mask */
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 );
744 /* end of recording session for a given dimension */
746 ps_dimension_end( PS_Dimension dim,
750 /* end hint mask table */
751 ps_dimension_end_mask( dim, end_point );
753 /* merge all counter masks into independent "paths" */
754 return ps_mask_table_merge_all( &dim->counters, memory );
758 /*************************************************************************/
759 /*************************************************************************/
761 /***** PS_RECORDER MANAGEMENT *****/
763 /*************************************************************************/
764 /*************************************************************************/
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;
791 /* initialize a hints for a new session */
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] );
804 /* add one or more stems to the current hints table */
806 ps_hints_stem( PS_Hints hints,
817 /* limit "dimension" to 0..1 */
820 FT_TRACE0(( "ps_hints_stem: invalid dimension (%d) used\n",
822 dimension = ( dimension != 0 );
825 /* record the stems in the current hints/masks table */
826 /* (Type 1 & 2's `hstem' or `vstem' operators) */
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;
852 /* add one Type1 counter stem to the current hints table */
854 ps_hints_t1stem3( T1_Hints hints_, /* PS_Hints */
858 PS_Hints hints = (PS_Hints)hints_;
859 FT_Error error = FT_Err_Ok;
865 FT_Memory memory = hints->memory;
870 /* limit "dimension" to 0..1 */
873 FT_TRACE0(( "ps_hints_t1stem3: invalid dimension (%d) used\n",
875 dimension = ( dimension != 0 );
878 dim = &hints->dimension[dimension];
880 /* there must be 6 elements in the 'stem' array */
881 if ( hints->hint_type == PS_HINT_TYPE_1 )
883 /* add the three stems to our hints/masks table */
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] );
894 /* now, add the hints to the counters table */
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;
916 /* reset hints (only with Type 1 hints) */
918 ps_hints_t1reset( T1_Hints hints_, /* PS_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],
944 /* invalid hint type */
945 error = FT_THROW( Invalid_Argument );
952 hints->error = error;
956 /* Type2 "hintmask" operator, add a new hintmask to each direction */
958 ps_hints_t2mask( T2_Hints hints_, /* PS_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;
975 /* check bit count; must be equal to current total hint count */
976 if ( bit_count != count1 + count2 )
978 FT_TRACE0(( "ps_hints_t2mask:"
979 " called with invalid bitcount %d (instead of %d)\n",
980 bit_count, count1 + count2 ));
982 /* simply ignore the operator */
986 /* set-up new horizontal and vertical hint mask now */
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_, /* PS_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;
1021 /* check bit count, must be equal to current total hint count */
1022 if ( bit_count != count1 + count2 )
1024 FT_TRACE0(( "ps_hints_t2counter:"
1025 " called with invalid bitcount %d (instead of %d)\n",
1026 bit_count, count1 + count2 ));
1028 /* simply ignore the operator */
1032 /* set-up new horizontal and vertical hint mask now */
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;
1050 /* end recording session */
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;
1080 /*************************************************************************/
1081 /*************************************************************************/
1083 /***** TYPE 1 HINTS RECORDING INTERFACE *****/
1085 /*************************************************************************/
1086 /*************************************************************************/
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;
1140 /*************************************************************************/
1141 /*************************************************************************/
1143 /***** TYPE 2 HINTS RECORDING INTERFACE *****/
1145 /*************************************************************************/
1146 /*************************************************************************/
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;
1176 /* determine number of stems to write */
1181 /* compute integer stem positions in font units */
1182 for ( n = 0; n < count * 2; n++ )
1184 y = ADD_LONG( y, coords[n] );
1185 stems[n] = FIXED_TO_INT( y );
1188 /* compute lengths */
1189 for ( n = 0; n < count * 2; n += 2 )
1190 stems[n + 1] = stems[n + 1] - stems[n];
1192 /* add them to the current dimension */
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;