Bringing apdf from vendor into main branch.
[AROS-Contrib.git] / apdf / freetype2 / pshinter / pshrec.c
blob208cf78d425bd4bb525fbe5665d3aae3b595cd03
1 /***************************************************************************/
2 /* */
3 /* pshrec.c */
4 /* */
5 /* FreeType PostScript hints recorder (body). */
6 /* */
7 /* Copyright 2001, 2002 by */
8 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
9 /* */
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. */
15 /* */
16 /***************************************************************************/
19 #include <ft2build.h>
20 #include FT_FREETYPE_H
21 #include FT_INTERNAL_OBJECTS_H
22 #include FT_INTERNAL_DEBUG_H
23 #include "pshrec.h"
24 #include "pshalgo.h"
26 #undef FT_COMPONENT
27 #define FT_COMPONENT trace_pshrec
29 #ifdef DEBUG_HINTER
30 extern PS_Hints ps_debug_hints = 0;
31 extern int ps_debug_no_horz_hints = 0;
32 extern int ps_debug_no_vert_hints = 0;
33 #endif
36 /*************************************************************************/
37 /*************************************************************************/
38 /***** *****/
39 /***** PS_HINT MANAGEMENT *****/
40 /***** *****/
41 /*************************************************************************/
42 /*************************************************************************/
44 /* destroy hints table */
45 static void
46 ps_hint_table_done( PS_Hint_Table table,
47 FT_Memory memory )
49 FT_FREE( table->hints );
50 table->num_hints = 0;
51 table->max_hints = 0;
55 /* ensure that a table can contain "count" elements */
56 static FT_Error
57 ps_hint_table_ensure( PS_Hint_Table table,
58 FT_UInt count,
59 FT_Memory memory )
61 FT_UInt old_max = table->max_hints;
62 FT_UInt new_max = count;
63 FT_Error error = 0;
66 if ( new_max > old_max )
68 /* try to grow the table */
69 new_max = ( new_max + 7 ) & -8;
70 if ( !FT_RENEW_ARRAY( table->hints, old_max, new_max ) )
71 table->max_hints = new_max;
73 return error;
77 static FT_Error
78 ps_hint_table_alloc( PS_Hint_Table table,
79 FT_Memory memory,
80 PS_Hint *ahint )
82 FT_Error error = 0;
83 FT_UInt count;
84 PS_Hint hint = 0;
87 count = table->num_hints;
88 count++;
90 if ( count >= table->max_hints )
92 error = ps_hint_table_ensure( table, count, memory );
93 if ( error )
94 goto Exit;
97 hint = table->hints + count - 1;
98 hint->pos = 0;
99 hint->len = 0;
100 hint->flags = 0;
102 table->num_hints = count;
104 Exit:
105 *ahint = hint;
106 return error;
110 /*************************************************************************/
111 /*************************************************************************/
112 /***** *****/
113 /***** PS_MASK MANAGEMENT *****/
114 /***** *****/
115 /*************************************************************************/
116 /*************************************************************************/
118 /* destroy mask */
119 static void
120 ps_mask_done( PS_Mask mask,
121 FT_Memory memory )
123 FT_FREE( mask->bytes );
124 mask->num_bits = 0;
125 mask->max_bits = 0;
126 mask->end_point = 0;
130 /* ensure that a mask can contain "count" bits */
131 static FT_Error
132 ps_mask_ensure( PS_Mask mask,
133 FT_UInt count,
134 FT_Memory memory )
136 FT_UInt old_max = ( mask->max_bits + 7 ) >> 3;
137 FT_UInt new_max = ( count + 7 ) >> 3;
138 FT_Error error = 0;
141 if ( new_max > old_max )
143 new_max = ( new_max + 7 ) & -8;
144 if ( !FT_RENEW_ARRAY( mask->bytes, old_max, new_max ) )
145 mask->max_bits = new_max * 8;
147 return error;
151 /* test a bit value in a given mask */
152 static FT_Int
153 ps_mask_test_bit( PS_Mask mask,
154 FT_Int idx )
156 if ( (FT_UInt)idx >= mask->num_bits )
157 return 0;
159 return mask->bytes[idx >> 3] & ( 0x80 >> ( idx & 7 ) );
163 /* clear a given bit */
164 static void
165 ps_mask_clear_bit( PS_Mask mask,
166 FT_Int idx )
168 FT_Byte* p;
171 if ( (FT_UInt)idx >= mask->num_bits )
172 return;
174 p = mask->bytes + ( idx >> 3 );
175 p[0] = (FT_Byte)( p[0] & ~( 0x80 >> ( idx & 7 ) ) );
179 /* set a given bit, possibly grow the mask */
180 static FT_Error
181 ps_mask_set_bit( PS_Mask mask,
182 FT_Int idx,
183 FT_Memory memory )
185 FT_Error error = 0;
186 FT_Byte* p;
189 if ( idx < 0 )
190 goto Exit;
192 if ( (FT_UInt)idx >= mask->num_bits )
194 error = ps_mask_ensure( mask, idx + 1, memory );
195 if ( error )
196 goto Exit;
198 mask->num_bits = idx + 1;
201 p = mask->bytes + ( idx >> 3 );
202 p[0] = (FT_Byte)( p[0] | ( 0x80 >> ( idx & 7 ) ) );
204 Exit:
205 return error;
209 /* destroy mask table */
210 static void
211 ps_mask_table_done( PS_Mask_Table table,
212 FT_Memory memory )
214 FT_UInt count = table->max_masks;
215 PS_Mask mask = table->masks;
218 for ( ; count > 0; count--, mask++ )
219 ps_mask_done( mask, memory );
221 FT_FREE( table->masks );
222 table->num_masks = 0;
223 table->max_masks = 0;
227 /* ensure that a mask table can contain "count" masks */
228 static FT_Error
229 ps_mask_table_ensure( PS_Mask_Table table,
230 FT_UInt count,
231 FT_Memory memory )
233 FT_UInt old_max = table->max_masks;
234 FT_UInt new_max = count;
235 FT_Error error = 0;
238 if ( new_max > old_max )
240 new_max = ( new_max + 7 ) & -8;
241 if ( !FT_RENEW_ARRAY( table->masks, old_max, new_max ) )
242 table->max_masks = new_max;
244 return error;
248 /* allocate a new mask in a table */
249 static FT_Error
250 ps_mask_table_alloc( PS_Mask_Table table,
251 FT_Memory memory,
252 PS_Mask *amask )
254 FT_UInt count;
255 FT_Error error = 0;
256 PS_Mask mask = 0;
259 count = table->num_masks;
260 count++;
262 if ( count > table->max_masks )
264 error = ps_mask_table_ensure( table, count, memory );
265 if ( error )
266 goto Exit;
269 mask = table->masks + count - 1;
270 mask->num_bits = 0;
271 mask->end_point = 0;
272 table->num_masks = count;
274 Exit:
275 *amask = mask;
276 return error;
280 /* return last hint mask in a table, create one if the table is empty */
281 static FT_Error
282 ps_mask_table_last( PS_Mask_Table table,
283 FT_Memory memory,
284 PS_Mask *amask )
286 FT_Error error = 0;
287 FT_UInt count;
288 PS_Mask mask;
291 count = table->num_masks;
292 if ( count == 0 )
294 error = ps_mask_table_alloc( table, memory, &mask );
295 if ( error )
296 goto Exit;
298 else
299 mask = table->masks + count - 1;
301 Exit:
302 *amask = mask;
303 return error;
307 /* set a new mask to a given bit range */
308 static FT_Error
309 ps_mask_table_set_bits( PS_Mask_Table table,
310 FT_Byte* source,
311 FT_UInt bit_pos,
312 FT_UInt bit_count,
313 FT_Memory memory )
315 FT_Error error = 0;
316 PS_Mask mask;
319 /* allocate new mask, and grow it to "bit_count" bits */
320 error = ps_mask_table_alloc( table, memory, &mask );
321 if ( error )
322 goto Exit;
324 error = ps_mask_ensure( mask, bit_count, memory );
325 if ( error )
326 goto Exit;
328 mask->num_bits = bit_count;
330 /* now, copy bits */
332 FT_Byte* read = source + ( bit_pos >> 3 );
333 FT_Int rmask = 0x80 >> ( bit_pos & 7 );
334 FT_Byte* write = mask->bytes;
335 FT_Int wmask = 0x80;
336 FT_Int val;
339 for ( ; bit_count > 0; bit_count-- )
341 val = write[0] & ~wmask;
343 if ( read[0] & rmask )
344 val |= wmask;
346 write[0] = (FT_Byte)val;
348 rmask >>= 1;
349 if ( rmask == 0 )
351 read++;
352 rmask = 0x80;
355 wmask >>= 1;
356 if ( wmask == 0 )
358 write++;
359 wmask = 0x80;
364 Exit:
365 return error;
369 /* test whether two masks in a table intersect */
370 static FT_Int
371 ps_mask_table_test_intersect( PS_Mask_Table table,
372 FT_Int index1,
373 FT_Int index2 )
375 PS_Mask mask1 = table->masks + index1;
376 PS_Mask mask2 = table->masks + index2;
377 FT_Byte* p1 = mask1->bytes;
378 FT_Byte* p2 = mask2->bytes;
379 FT_UInt count1 = mask1->num_bits;
380 FT_UInt count2 = mask2->num_bits;
381 FT_UInt count;
384 count = ( count1 <= count2 ) ? count1 : count2;
385 for ( ; count >= 8; count -= 8 )
387 if ( p1[0] & p2[0] )
388 return 1;
390 p1++;
391 p2++;
394 if ( count == 0 )
395 return 0;
397 return ( p1[0] & p2[0] ) & ~( 0xFF >> count );
401 /* merge two masks, used by ps_mask_table_merge_all */
402 static FT_Error
403 ps_mask_table_merge( PS_Mask_Table table,
404 FT_Int index1,
405 FT_Int index2,
406 FT_Memory memory )
408 FT_UInt temp;
409 FT_Error error = 0;
412 /* swap index1 and index2 so that index1 < index2 */
413 if ( index1 > index2 )
415 temp = index1;
416 index1 = index2;
417 index2 = temp;
420 if ( index1 < index2 && index1 >= 0 && index2 < (FT_Int)table->num_masks )
422 /* we need to merge the bitsets of index1 and index2 with a */
423 /* simple union */
424 PS_Mask mask1 = table->masks + index1;
425 PS_Mask mask2 = table->masks + index2;
426 FT_UInt count1 = mask1->num_bits;
427 FT_UInt count2 = mask2->num_bits;
428 FT_Int delta;
431 if ( count2 > 0 )
433 FT_UInt pos;
434 FT_Byte* read;
435 FT_Byte* write;
438 /* if "count2" is greater than "count1", we need to grow the */
439 /* first bitset, and clear the highest bits */
440 if ( count2 > count1 )
442 error = ps_mask_ensure( mask1, count2, memory );
443 if ( error )
444 goto Exit;
446 for ( pos = count1; pos < count2; pos++ )
447 ps_mask_clear_bit( mask1, pos );
450 /* merge (unite) the bitsets */
451 read = mask2->bytes;
452 write = mask1->bytes;
453 pos = (FT_UInt)( ( count2 + 7 ) >> 3 );
455 for ( ; pos > 0; pos-- )
457 write[0] = (FT_Byte)( write[0] | read[0] );
458 write++;
459 read++;
463 /* Now, remove "mask2" from the list. We need to keep the masks */
464 /* sorted in order of importance, so move table elements. */
465 mask2->num_bits = 0;
466 mask2->end_point = 0;
468 delta = table->num_masks - 1 - index2; /* number of masks to move */
469 if ( delta > 0 )
471 /* move to end of table for reuse */
472 PS_MaskRec dummy = *mask2;
475 ft_memmove( mask2, mask2 + 1, delta * sizeof ( PS_MaskRec ) );
477 mask2[delta] = dummy;
480 table->num_masks--;
482 else
483 FT_ERROR(( "ps_mask_table_merge: ignoring invalid indices (%d,%d)\n",
484 index1, index2 ));
486 Exit:
487 return error;
491 /* Try to merge all masks in a given table. This is used to merge */
492 /* all counter masks into independent counter "paths". */
493 /* */
494 static FT_Error
495 ps_mask_table_merge_all( PS_Mask_Table table,
496 FT_Memory memory )
498 FT_Int index1, index2;
499 FT_Error error = 0;
502 for ( index1 = table->num_masks - 1; index1 > 0; index1-- )
504 for ( index2 = index1 - 1; index2 >= 0; index2-- )
506 if ( ps_mask_table_test_intersect( table, index1, index2 ) )
508 error = ps_mask_table_merge( table, index2, index1, memory );
509 if ( error )
510 goto Exit;
512 break;
517 Exit:
518 return error;
522 /*************************************************************************/
523 /*************************************************************************/
524 /***** *****/
525 /***** PS_DIMENSION MANAGEMENT *****/
526 /***** *****/
527 /*************************************************************************/
528 /*************************************************************************/
531 /* finalize a given dimension */
532 static void
533 ps_dimension_done( PS_Dimension dimension,
534 FT_Memory memory )
536 ps_mask_table_done( &dimension->counters, memory );
537 ps_mask_table_done( &dimension->masks, memory );
538 ps_hint_table_done( &dimension->hints, memory );
542 /* initialize a given dimension */
543 static void
544 ps_dimension_init( PS_Dimension dimension )
546 dimension->hints.num_hints = 0;
547 dimension->masks.num_masks = 0;
548 dimension->counters.num_masks = 0;
552 #if 0
554 /* set a bit at a given index in the current hint mask */
555 static FT_Error
556 ps_dimension_set_mask_bit( PS_Dimension dim,
557 FT_UInt idx,
558 FT_Memory memory )
560 PS_Mask mask;
561 FT_Error error = 0;
564 /* get last hint mask */
565 error = ps_mask_table_last( &dim->masks, memory, &mask );
566 if ( error )
567 goto Exit;
569 error = ps_mask_set_bit( mask, idx, memory );
571 Exit:
572 return error;
575 #endif
577 /* set the end point in a mask, called from "End" & "Reset" methods */
578 static void
579 ps_dimension_end_mask( PS_Dimension dim,
580 FT_UInt end_point )
582 FT_UInt count = dim->masks.num_masks;
583 PS_Mask mask;
586 if ( count > 0 )
588 mask = dim->masks.masks + count - 1;
589 mask->end_point = end_point;
594 /* set the end point in the current mask, then create a new empty one */
595 /* (called by "Reset" method) */
596 static FT_Error
597 ps_dimension_reset_mask( PS_Dimension dim,
598 FT_UInt end_point,
599 FT_Memory memory )
601 PS_Mask mask;
604 /* end current mask */
605 ps_dimension_end_mask( dim, end_point );
607 /* allocate new one */
608 return ps_mask_table_alloc( &dim->masks, memory, &mask );
612 /* set a new mask, called from the "T2Stem" method */
613 static FT_Error
614 ps_dimension_set_mask_bits( PS_Dimension dim,
615 const FT_Byte* source,
616 FT_UInt source_pos,
617 FT_UInt source_bits,
618 FT_UInt end_point,
619 FT_Memory memory )
621 FT_Error error = 0;
624 /* reset current mask, if any */
625 error = ps_dimension_reset_mask( dim, end_point, memory );
626 if ( error )
627 goto Exit;
629 /* set bits in new mask */
630 error = ps_mask_table_set_bits( &dim->masks, (FT_Byte*)source,
631 source_pos, source_bits, memory );
633 Exit:
634 return error;
638 /* add a new single stem (called from "T1Stem" method) */
639 static FT_Error
640 ps_dimension_add_t1stem( PS_Dimension dim,
641 FT_Int pos,
642 FT_Int len,
643 FT_Memory memory,
644 FT_Int *aindex )
646 FT_Error error = 0;
647 FT_UInt flags = 0;
650 /* detect ghost stem */
651 if ( len < 0 )
653 flags |= PS_HINT_FLAG_GHOST;
654 if ( len == -21 )
656 flags |= PS_HINT_FLAG_BOTTOM;
657 pos += len;
659 len = 0;
662 if ( aindex )
663 *aindex = -1;
665 /* now, lookup stem in the current hints table */
667 PS_Mask mask;
668 FT_UInt idx;
669 FT_UInt max = dim->hints.num_hints;
670 PS_Hint hint = dim->hints.hints;
673 for ( idx = 0; idx < max; idx++, hint++ )
675 if ( hint->pos == pos && hint->len == len )
676 break;
679 /* we need to create a new hint in the table */
680 if ( idx >= max )
682 error = ps_hint_table_alloc( &dim->hints, memory, &hint );
683 if ( error )
684 goto Exit;
686 hint->pos = pos;
687 hint->len = len;
688 hint->flags = flags;
691 /* now, store the hint in the current mask */
692 error = ps_mask_table_last( &dim->masks, memory, &mask );
693 if ( error )
694 goto Exit;
696 error = ps_mask_set_bit( mask, idx, memory );
697 if ( error )
698 goto Exit;
700 if ( aindex )
701 *aindex = (FT_Int)idx;
704 Exit:
705 return error;
709 /* add a "hstem3/vstem3" counter to our dimension table */
710 static FT_Error
711 ps_dimension_add_counter( PS_Dimension dim,
712 FT_Int hint1,
713 FT_Int hint2,
714 FT_Int hint3,
715 FT_Memory memory )
717 FT_Error error = 0;
718 FT_UInt count = dim->counters.num_masks;
719 PS_Mask counter = dim->counters.masks;
722 /* try to find an existing counter mask that already uses */
723 /* one of these stems here */
724 for ( ; count > 0; count--, counter++ )
726 if ( ps_mask_test_bit( counter, hint1 ) ||
727 ps_mask_test_bit( counter, hint2 ) ||
728 ps_mask_test_bit( counter, hint3 ) )
729 break;
732 /* creat a new counter when needed */
733 if ( count == 0 )
735 error = ps_mask_table_alloc( &dim->counters, memory, &counter );
736 if ( error )
737 goto Exit;
740 /* now, set the bits for our hints in the counter mask */
741 error = ps_mask_set_bit( counter, hint1, memory );
742 if ( error )
743 goto Exit;
745 error = ps_mask_set_bit( counter, hint2, memory );
746 if ( error )
747 goto Exit;
749 error = ps_mask_set_bit( counter, hint3, memory );
750 if ( error )
751 goto Exit;
753 Exit:
754 return error;
758 /* end of recording session for a given dimension */
759 static FT_Error
760 ps_dimension_end( PS_Dimension dim,
761 FT_UInt end_point,
762 FT_Memory memory )
764 /* end hint mask table */
765 ps_dimension_end_mask( dim, end_point );
767 /* merge all counter masks into independent "paths" */
768 return ps_mask_table_merge_all( &dim->counters, memory );
772 /*************************************************************************/
773 /*************************************************************************/
774 /***** *****/
775 /***** PS_RECORDER MANAGEMENT *****/
776 /***** *****/
777 /*************************************************************************/
778 /*************************************************************************/
781 /* destroy hints */
782 FT_LOCAL( void )
783 ps_hints_done( PS_Hints hints )
785 FT_Memory memory = hints->memory;
788 ps_dimension_done( &hints->dimension[0], memory );
789 ps_dimension_done( &hints->dimension[1], memory );
791 hints->error = 0;
792 hints->memory = 0;
796 FT_LOCAL( FT_Error )
797 ps_hints_init( PS_Hints hints,
798 FT_Memory memory )
800 ft_memset( hints, 0, sizeof ( *hints ) );
801 hints->memory = memory;
802 return 0;
806 /* initialize a hints for a new session */
807 static void
808 ps_hints_open( PS_Hints hints,
809 PS_Hint_Type hint_type )
811 switch ( hint_type )
813 case PS_HINT_TYPE_1:
814 case PS_HINT_TYPE_2:
815 hints->error = 0;
816 hints->hint_type = hint_type;
818 ps_dimension_init( &hints->dimension[0] );
819 ps_dimension_init( &hints->dimension[1] );
820 break;
822 default:
823 hints->error = FT_Err_Invalid_Argument;
824 hints->hint_type = hint_type;
826 FT_ERROR(( "ps_hints_open: invalid charstring type!\n" ));
827 break;
832 /* add one or more stems to the current hints table */
833 static void
834 ps_hints_stem( PS_Hints hints,
835 FT_Int dimension,
836 FT_UInt count,
837 FT_Long* stems )
839 if ( !hints->error )
841 /* limit "dimension" to 0..1 */
842 if ( dimension < 0 || dimension > 1 )
844 FT_ERROR(( "ps_hints_stem: invalid dimension (%d) used\n",
845 dimension ));
846 dimension = ( dimension != 0 );
849 /* record the stems in the current hints/masks table */
850 switch ( hints->hint_type )
852 case PS_HINT_TYPE_1: /* Type 1 "hstem" or "vstem" operator */
853 case PS_HINT_TYPE_2: /* Type 2 "hstem" or "vstem" operator */
855 PS_Dimension dim = &hints->dimension[dimension];
858 for ( ; count > 0; count--, stems += 2 )
860 FT_Error error;
861 FT_Memory memory = hints->memory;
864 error = ps_dimension_add_t1stem( dim, stems[0], stems[1],
865 memory, NULL );
866 if ( error )
868 FT_ERROR(( "ps_hints_stem: could not add stem"
869 " (%d,%d) to hints table\n", stems[0], stems[1] ));
871 hints->error = error;
872 return;
875 break;
878 default:
879 FT_ERROR(( "ps_hints_stem: called with invalid hint type (%d)\n",
880 hints->hint_type ));
881 break;
887 /* add one Type1 counter stem to the current hints table */
888 static void
889 ps_hints_t1stem3( PS_Hints hints,
890 FT_Int dimension,
891 FT_Long* stems )
893 FT_Error error = 0;
896 if ( !hints->error )
898 PS_Dimension dim;
899 FT_Memory memory = hints->memory;
900 FT_Int count;
901 FT_Int idx[3];
904 /* limit "dimension" to 0..1 */
905 if ( dimension < 0 || dimension > 1 )
907 FT_ERROR(( "ps_hints_t1stem3: invalid dimension (%d) used\n",
908 dimension ));
909 dimension = ( dimension != 0 );
912 dim = &hints->dimension[dimension];
914 /* there must be 6 elements in the 'stem' array */
915 if ( hints->hint_type == PS_HINT_TYPE_1 )
917 /* add the three stems to our hints/masks table */
918 for ( count = 0; count < 3; count++, stems += 2 )
920 error = ps_dimension_add_t1stem( dim, stems[0], stems[1],
921 memory, &idx[count] );
922 if ( error )
923 goto Fail;
926 /* now, add the hints to the counters table */
927 error = ps_dimension_add_counter( dim, idx[0], idx[1], idx[2],
928 memory );
929 if ( error )
930 goto Fail;
932 else
934 FT_ERROR(( "ps_hints_t1stem3: called with invalid hint type!\n" ));
935 error = FT_Err_Invalid_Argument;
936 goto Fail;
940 return;
942 Fail:
943 FT_ERROR(( "ps_hints_t1stem3: could not add counter stems to table\n" ));
944 hints->error = error;
948 /* reset hints (only with Type 1 hints) */
949 static void
950 ps_hints_t1reset( PS_Hints hints,
951 FT_UInt end_point )
953 FT_Error error = 0;
956 if ( !hints->error )
958 FT_Memory memory = hints->memory;
961 if ( hints->hint_type == PS_HINT_TYPE_1 )
963 error = ps_dimension_reset_mask( &hints->dimension[0],
964 end_point, memory );
965 if ( error )
966 goto Fail;
968 error = ps_dimension_reset_mask( &hints->dimension[1],
969 end_point, memory );
970 if ( error )
971 goto Fail;
973 else
975 /* invalid hint type */
976 error = FT_Err_Invalid_Argument;
977 goto Fail;
980 return;
982 Fail:
983 hints->error = error;
987 /* Type2 "hintmask" operator, add a new hintmask to each direction */
988 static void
989 ps_hints_t2mask( PS_Hints hints,
990 FT_UInt end_point,
991 FT_UInt bit_count,
992 const FT_Byte* bytes )
994 FT_Error error;
997 if ( !hints->error )
999 PS_Dimension dim = hints->dimension;
1000 FT_Memory memory = hints->memory;
1001 FT_UInt count1 = dim[0].hints.num_hints;
1002 FT_UInt count2 = dim[1].hints.num_hints;
1005 /* check bit count; must be equal to current total hint count */
1006 if ( bit_count != count1 + count2 )
1008 FT_ERROR(( "ps_hints_t2mask: "
1009 "called with invalid bitcount %d (instead of %d)\n",
1010 bit_count, count1 + count2 ));
1012 /* simply ignore the operator */
1013 return;
1016 /* set-up new horizontal and vertical hint mask now */
1017 error = ps_dimension_set_mask_bits( &dim[0], bytes, 0, count1,
1018 end_point, memory );
1019 if ( error )
1020 goto Fail;
1022 error = ps_dimension_set_mask_bits( &dim[1], bytes, count1, count2,
1023 end_point, memory );
1024 if ( error )
1025 goto Fail;
1027 return;
1029 Fail:
1030 hints->error = error;
1034 static void
1035 ps_hints_t2counter( PS_Hints hints,
1036 FT_UInt bit_count,
1037 const FT_Byte* bytes )
1039 FT_Error error;
1042 if ( !hints->error )
1044 PS_Dimension dim = hints->dimension;
1045 FT_Memory memory = hints->memory;
1046 FT_UInt count1 = dim[0].hints.num_hints;
1047 FT_UInt count2 = dim[1].hints.num_hints;
1050 /* check bit count, must be equal to current total hint count */
1051 if ( bit_count != count1 + count2 )
1053 FT_ERROR(( "ps_hints_t2counter: "
1054 "called with invalid bitcount %d (instead of %d)\n",
1055 bit_count, count1 + count2 ));
1057 /* simply ignore the operator */
1058 return;
1061 /* set-up new horizontal and vertical hint mask now */
1062 error = ps_dimension_set_mask_bits( &dim[0], bytes, 0, count1,
1063 0, memory );
1064 if ( error )
1065 goto Fail;
1067 error = ps_dimension_set_mask_bits( &dim[1], bytes, count1, count2,
1068 0, memory );
1069 if ( error )
1070 goto Fail;
1072 return;
1074 Fail:
1075 hints->error = error;
1079 /* end recording session */
1080 static FT_Error
1081 ps_hints_close( PS_Hints hints,
1082 FT_UInt end_point )
1084 FT_Error error;
1087 error = hints->error;
1088 if ( !error )
1090 FT_Memory memory = hints->memory;
1091 PS_Dimension dim = hints->dimension;
1094 error = ps_dimension_end( &dim[0], end_point, memory );
1095 if ( !error )
1097 error = ps_dimension_end( &dim[1], end_point, memory );
1101 #ifdef DEBUG_HINTER
1102 if ( !error )
1103 ps_debug_hints = hints;
1104 #endif
1105 return error;
1109 /*************************************************************************/
1110 /*************************************************************************/
1111 /***** *****/
1112 /***** TYPE 1 HINTS RECORDING INTERFACE *****/
1113 /***** *****/
1114 /*************************************************************************/
1115 /*************************************************************************/
1117 static void
1118 t1_hints_open( T1_Hints hints )
1120 ps_hints_open( (PS_Hints)hints, PS_HINT_TYPE_1 );
1123 static void
1124 t1_hints_stem( T1_Hints hints,
1125 FT_Int dimension,
1126 FT_Long* coords )
1128 ps_hints_stem( (PS_Hints)hints, dimension, 1, coords );
1132 FT_LOCAL_DEF( void )
1133 t1_hints_funcs_init( T1_Hints_FuncsRec* funcs )
1135 ft_memset( (char*)funcs, 0, sizeof ( *funcs ) );
1137 funcs->open = (T1_Hints_OpenFunc) t1_hints_open;
1138 funcs->close = (T1_Hints_CloseFunc) ps_hints_close;
1139 funcs->stem = (T1_Hints_SetStemFunc) t1_hints_stem;
1140 funcs->stem3 = (T1_Hints_SetStem3Func)ps_hints_t1stem3;
1141 funcs->reset = (T1_Hints_ResetFunc) ps_hints_t1reset;
1142 funcs->apply = (T1_Hints_ApplyFunc) PS_HINTS_APPLY_FUNC;
1146 /*************************************************************************/
1147 /*************************************************************************/
1148 /***** *****/
1149 /***** TYPE 2 HINTS RECORDING INTERFACE *****/
1150 /***** *****/
1151 /*************************************************************************/
1152 /*************************************************************************/
1154 static void
1155 t2_hints_open( T2_Hints hints )
1157 ps_hints_open( (PS_Hints)hints, PS_HINT_TYPE_2 );
1161 static void
1162 t2_hints_stems( T2_Hints hints,
1163 FT_Int dimension,
1164 FT_Int count,
1165 FT_Fixed* coords )
1167 FT_Pos stems[32], y, n, total = count;
1170 y = 0;
1171 while ( total > 0 )
1173 /* determine number of stems to write */
1174 count = total;
1175 if ( count > 16 )
1176 count = 16;
1178 /* compute integer stem positions in font units */
1179 for ( n = 0; n < count * 2; n++ )
1181 y += coords[n];
1182 stems[n] = ( y + 0x8000 ) >> 16;
1185 /* compute lengths */
1186 for ( n = 0; n < count * 2; n += 2 )
1187 stems[n + 1] = stems[n + 1] - stems[n];
1189 /* add them to the current dimension */
1190 ps_hints_stem( (PS_Hints)hints, dimension, count, stems );
1192 total -= count;
1197 FT_LOCAL_DEF( void )
1198 t2_hints_funcs_init( T2_Hints_FuncsRec* funcs )
1200 ft_memset( funcs, 0, sizeof ( *funcs ) );
1202 funcs->open = (T2_Hints_OpenFunc) t2_hints_open;
1203 funcs->close = (T2_Hints_CloseFunc) ps_hints_close;
1204 funcs->stems = (T2_Hints_StemsFunc) t2_hints_stems;
1205 funcs->hintmask= (T2_Hints_MaskFunc) ps_hints_t2mask;
1206 funcs->counter = (T2_Hints_CounterFunc)ps_hints_t2counter;
1207 funcs->apply = (T2_Hints_ApplyFunc) PS_HINTS_APPLY_FUNC;
1211 /* END */