xref: /inferno-os/libfreetype/pshrec.c (revision 37da2899f40661e3e9631e497da8dc59b971cbd0)
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 /***************************************************************************/
17 
18 
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"
25 
26 #undef  FT_COMPONENT
27 #define FT_COMPONENT  trace_pshrec
28 
29 #ifdef DEBUG_HINTER
30   PS_Hints  ps_debug_hints         = 0;
31   int       ps_debug_no_horz_hints = 0;
32   int       ps_debug_no_vert_hints = 0;
33 #endif
34 
35 
36   /*************************************************************************/
37   /*************************************************************************/
38   /*****                                                               *****/
39   /*****                      PS_HINT MANAGEMENT                       *****/
40   /*****                                                               *****/
41   /*************************************************************************/
42   /*************************************************************************/
43 
44   /* destroy hints table */
45   static void
ps_hint_table_done(PS_Hint_Table table,FT_Memory memory)46   ps_hint_table_done( PS_Hint_Table  table,
47                       FT_Memory      memory )
48   {
49     FT_FREE( table->hints );
50     table->num_hints = 0;
51     table->max_hints = 0;
52   }
53 
54 
55   /* ensure that a table can contain "count" elements */
56   static FT_Error
ps_hint_table_ensure(PS_Hint_Table table,FT_UInt count,FT_Memory memory)57   ps_hint_table_ensure( PS_Hint_Table  table,
58                         FT_UInt        count,
59                         FT_Memory      memory )
60   {
61     FT_UInt   old_max = table->max_hints;
62     FT_UInt   new_max = count;
63     FT_Error  error   = 0;
64 
65 
66     if ( new_max > old_max )
67     {
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;
72     }
73     return error;
74   }
75 
76 
77   static FT_Error
ps_hint_table_alloc(PS_Hint_Table table,FT_Memory memory,PS_Hint * ahint)78   ps_hint_table_alloc( PS_Hint_Table  table,
79                        FT_Memory      memory,
80                        PS_Hint       *ahint )
81   {
82     FT_Error  error = 0;
83     FT_UInt   count;
84     PS_Hint   hint = 0;
85 
86 
87     count = table->num_hints;
88     count++;
89 
90     if ( count >= table->max_hints )
91     {
92       error = ps_hint_table_ensure( table, count, memory );
93       if ( error )
94         goto Exit;
95     }
96 
97     hint        = table->hints + count - 1;
98     hint->pos   = 0;
99     hint->len   = 0;
100     hint->flags = 0;
101 
102     table->num_hints = count;
103 
104   Exit:
105     *ahint = hint;
106     return error;
107   }
108 
109 
110   /*************************************************************************/
111   /*************************************************************************/
112   /*****                                                               *****/
113   /*****                      PS_MASK MANAGEMENT                       *****/
114   /*****                                                               *****/
115   /*************************************************************************/
116   /*************************************************************************/
117 
118   /* destroy mask */
119   static void
ps_mask_done(PS_Mask mask,FT_Memory memory)120   ps_mask_done( PS_Mask    mask,
121                 FT_Memory  memory )
122   {
123     FT_FREE( mask->bytes );
124     mask->num_bits  = 0;
125     mask->max_bits  = 0;
126     mask->end_point = 0;
127   }
128 
129 
130   /* ensure that a mask can contain "count" bits */
131   static FT_Error
ps_mask_ensure(PS_Mask mask,FT_UInt count,FT_Memory memory)132   ps_mask_ensure( PS_Mask    mask,
133                   FT_UInt    count,
134                   FT_Memory  memory )
135   {
136     FT_UInt   old_max = ( mask->max_bits + 7 ) >> 3;
137     FT_UInt   new_max = ( count          + 7 ) >> 3;
138     FT_Error  error   = 0;
139 
140 
141     if ( new_max > old_max )
142     {
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;
146     }
147     return error;
148   }
149 
150 
151   /* test a bit value in a given mask */
152   static FT_Int
ps_mask_test_bit(PS_Mask mask,FT_Int idx)153   ps_mask_test_bit( PS_Mask  mask,
154                     FT_Int   idx )
155   {
156     if ( (FT_UInt)idx >= mask->num_bits )
157       return 0;
158 
159     return mask->bytes[idx >> 3] & ( 0x80 >> ( idx & 7 ) );
160   }
161 
162 
163   /* clear a given bit */
164   static void
ps_mask_clear_bit(PS_Mask mask,FT_Int idx)165   ps_mask_clear_bit( PS_Mask  mask,
166                      FT_Int   idx )
167   {
168     FT_Byte*  p;
169 
170 
171     if ( (FT_UInt)idx >= mask->num_bits )
172       return;
173 
174     p    = mask->bytes + ( idx >> 3 );
175     p[0] = (FT_Byte)( p[0] & ~( 0x80 >> ( idx & 7 ) ) );
176   }
177 
178 
179   /* set a given bit, possibly grow the mask */
180   static FT_Error
ps_mask_set_bit(PS_Mask mask,FT_Int idx,FT_Memory memory)181   ps_mask_set_bit( PS_Mask    mask,
182                    FT_Int     idx,
183                    FT_Memory  memory )
184   {
185     FT_Error  error = 0;
186     FT_Byte*  p;
187 
188 
189     if ( idx < 0 )
190       goto Exit;
191 
192     if ( (FT_UInt)idx >= mask->num_bits )
193     {
194       error = ps_mask_ensure( mask, idx + 1, memory );
195       if ( error )
196         goto Exit;
197 
198       mask->num_bits = idx + 1;
199     }
200 
201     p    = mask->bytes + ( idx >> 3 );
202     p[0] = (FT_Byte)( p[0] | ( 0x80 >> ( idx & 7 ) ) );
203 
204   Exit:
205     return error;
206   }
207 
208 
209   /* destroy mask table */
210   static void
ps_mask_table_done(PS_Mask_Table table,FT_Memory memory)211   ps_mask_table_done( PS_Mask_Table  table,
212                       FT_Memory      memory )
213   {
214     FT_UInt  count = table->max_masks;
215     PS_Mask  mask  = table->masks;
216 
217 
218     for ( ; count > 0; count--, mask++ )
219       ps_mask_done( mask, memory );
220 
221     FT_FREE( table->masks );
222     table->num_masks = 0;
223     table->max_masks = 0;
224   }
225 
226 
227   /* ensure that a mask table can contain "count" masks */
228   static FT_Error
ps_mask_table_ensure(PS_Mask_Table table,FT_UInt count,FT_Memory memory)229   ps_mask_table_ensure( PS_Mask_Table  table,
230                         FT_UInt        count,
231                         FT_Memory      memory )
232   {
233     FT_UInt   old_max = table->max_masks;
234     FT_UInt   new_max = count;
235     FT_Error  error   = 0;
236 
237 
238     if ( new_max > old_max )
239     {
240       new_max = ( new_max + 7 ) & -8;
241       if ( !FT_RENEW_ARRAY( table->masks, old_max, new_max ) )
242         table->max_masks = new_max;
243     }
244     return error;
245   }
246 
247 
248   /* allocate a new mask in a table */
249   static FT_Error
ps_mask_table_alloc(PS_Mask_Table table,FT_Memory memory,PS_Mask * amask)250   ps_mask_table_alloc( PS_Mask_Table  table,
251                        FT_Memory      memory,
252                        PS_Mask       *amask )
253   {
254     FT_UInt   count;
255     FT_Error  error = 0;
256     PS_Mask   mask  = 0;
257 
258 
259     count = table->num_masks;
260     count++;
261 
262     if ( count > table->max_masks )
263     {
264       error = ps_mask_table_ensure( table, count, memory );
265       if ( error )
266         goto Exit;
267     }
268 
269     mask             = table->masks + count - 1;
270     mask->num_bits   = 0;
271     mask->end_point  = 0;
272     table->num_masks = count;
273 
274   Exit:
275     *amask = mask;
276     return error;
277   }
278 
279 
280   /* return last hint mask in a table, create one if the table is empty */
281   static FT_Error
ps_mask_table_last(PS_Mask_Table table,FT_Memory memory,PS_Mask * amask)282   ps_mask_table_last( PS_Mask_Table  table,
283                       FT_Memory      memory,
284                       PS_Mask       *amask )
285   {
286     FT_Error  error = 0;
287     FT_UInt   count;
288     PS_Mask   mask;
289 
290 
291     count = table->num_masks;
292     if ( count == 0 )
293     {
294       error = ps_mask_table_alloc( table, memory, &mask );
295       if ( error )
296         goto Exit;
297     }
298     else
299       mask = table->masks + count - 1;
300 
301   Exit:
302     *amask = mask;
303     return error;
304   }
305 
306 
307   /* set a new mask to a given bit range */
308   static FT_Error
ps_mask_table_set_bits(PS_Mask_Table table,FT_Byte * source,FT_UInt bit_pos,FT_UInt bit_count,FT_Memory memory)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 )
314   {
315     FT_Error  error = 0;
316     PS_Mask   mask;
317 
318 
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;
323 
324     error = ps_mask_ensure( mask, bit_count, memory );
325     if ( error )
326       goto Exit;
327 
328     mask->num_bits = bit_count;
329 
330     /* now, copy bits */
331     {
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;
337 
338 
339       for ( ; bit_count > 0; bit_count-- )
340       {
341         val = write[0] & ~wmask;
342 
343         if ( read[0] & rmask )
344           val |= wmask;
345 
346         write[0] = (FT_Byte)val;
347 
348         rmask >>= 1;
349         if ( rmask == 0 )
350         {
351           read++;
352           rmask = 0x80;
353         }
354 
355         wmask >>= 1;
356         if ( wmask == 0 )
357         {
358           write++;
359           wmask = 0x80;
360         }
361       }
362     }
363 
364   Exit:
365     return error;
366   }
367 
368 
369   /* test whether two masks in a table intersect */
370   static FT_Int
ps_mask_table_test_intersect(PS_Mask_Table table,FT_Int index1,FT_Int index2)371   ps_mask_table_test_intersect( PS_Mask_Table  table,
372                                 FT_Int         index1,
373                                 FT_Int         index2 )
374   {
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;
382 
383 
384     count = ( count1 <= count2 ) ? count1 : count2;
385     for ( ; count >= 8; count -= 8 )
386     {
387       if ( p1[0] & p2[0] )
388         return 1;
389 
390       p1++;
391       p2++;
392     }
393 
394     if ( count == 0 )
395       return 0;
396 
397     return ( p1[0] & p2[0] ) & ~( 0xFF >> count );
398   }
399 
400 
401   /* merge two masks, used by ps_mask_table_merge_all */
402   static FT_Error
ps_mask_table_merge(PS_Mask_Table table,FT_Int index1,FT_Int index2,FT_Memory memory)403   ps_mask_table_merge( PS_Mask_Table  table,
404                        FT_Int         index1,
405                        FT_Int         index2,
406                        FT_Memory      memory )
407   {
408     FT_UInt   temp;
409     FT_Error  error = 0;
410 
411 
412     /* swap index1 and index2 so that index1 < index2 */
413     if ( index1 > index2 )
414     {
415       temp   = index1;
416       index1 = index2;
417       index2 = temp;
418     }
419 
420     if ( index1 < index2 && index1 >= 0 && index2 < (FT_Int)table->num_masks )
421     {
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;
429 
430 
431       if ( count2 > 0 )
432       {
433         FT_UInt   pos;
434         FT_Byte*  read;
435         FT_Byte*  write;
436 
437 
438         /* if "count2" is greater than "count1", we need to grow the */
439         /* first bitset, and clear the highest bits                  */
440         if ( count2 > count1 )
441         {
442           error = ps_mask_ensure( mask1, count2, memory );
443           if ( error )
444             goto Exit;
445 
446           for ( pos = count1; pos < count2; pos++ )
447             ps_mask_clear_bit( mask1, pos );
448         }
449 
450         /* merge (unite) the bitsets */
451         read  = mask2->bytes;
452         write = mask1->bytes;
453         pos   = (FT_UInt)( ( count2 + 7 ) >> 3 );
454 
455         for ( ; pos > 0; pos-- )
456         {
457           write[0] = (FT_Byte)( write[0] | read[0] );
458           write++;
459           read++;
460         }
461       }
462 
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;
467 
468       delta = table->num_masks - 1 - index2; /* number of masks to move */
469       if ( delta > 0 )
470       {
471         /* move to end of table for reuse */
472         PS_MaskRec  dummy = *mask2;
473 
474 
475         ft_memmove( mask2, mask2 + 1, delta * sizeof ( PS_MaskRec ) );
476 
477         mask2[delta] = dummy;
478       }
479 
480       table->num_masks--;
481     }
482     else
483       FT_ERROR(( "ps_mask_table_merge: ignoring invalid indices (%d,%d)\n",
484                  index1, index2 ));
485 
486   Exit:
487     return error;
488   }
489 
490 
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
ps_mask_table_merge_all(PS_Mask_Table table,FT_Memory memory)495   ps_mask_table_merge_all( PS_Mask_Table  table,
496                            FT_Memory      memory )
497   {
498     FT_Int    index1, index2;
499     FT_Error  error = 0;
500 
501 
502     for ( index1 = table->num_masks - 1; index1 > 0; index1-- )
503     {
504       for ( index2 = index1 - 1; index2 >= 0; index2-- )
505       {
506         if ( ps_mask_table_test_intersect( table, index1, index2 ) )
507         {
508           error = ps_mask_table_merge( table, index2, index1, memory );
509           if ( error )
510             goto Exit;
511 
512           break;
513         }
514       }
515     }
516 
517   Exit:
518     return error;
519   }
520 
521 
522   /*************************************************************************/
523   /*************************************************************************/
524   /*****                                                               *****/
525   /*****                    PS_DIMENSION MANAGEMENT                    *****/
526   /*****                                                               *****/
527   /*************************************************************************/
528   /*************************************************************************/
529 
530 
531   /* finalize a given dimension */
532   static void
ps_dimension_done(PS_Dimension dimension,FT_Memory memory)533   ps_dimension_done( PS_Dimension  dimension,
534                      FT_Memory     memory )
535   {
536     ps_mask_table_done( &dimension->counters, memory );
537     ps_mask_table_done( &dimension->masks,    memory );
538     ps_hint_table_done( &dimension->hints,    memory );
539   }
540 
541 
542   /* initialize a given dimension */
543   static void
ps_dimension_init(PS_Dimension dimension)544   ps_dimension_init( PS_Dimension  dimension )
545   {
546     dimension->hints.num_hints    = 0;
547     dimension->masks.num_masks    = 0;
548     dimension->counters.num_masks = 0;
549   }
550 
551 
552 #if 0
553 
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 )
559   {
560     PS_Mask  mask;
561     FT_Error  error = 0;
562 
563 
564     /* get last hint mask */
565     error = ps_mask_table_last( &dim->masks, memory, &mask );
566     if ( error )
567       goto Exit;
568 
569     error = ps_mask_set_bit( mask, idx, memory );
570 
571   Exit:
572     return error;
573   }
574 
575 #endif
576 
577   /* set the end point in a mask, called from "End" & "Reset" methods */
578   static void
ps_dimension_end_mask(PS_Dimension dim,FT_UInt end_point)579   ps_dimension_end_mask( PS_Dimension  dim,
580                          FT_UInt       end_point )
581   {
582     FT_UInt  count = dim->masks.num_masks;
583     PS_Mask  mask;
584 
585 
586     if ( count > 0 )
587     {
588       mask            = dim->masks.masks + count - 1;
589       mask->end_point = end_point;
590     }
591   }
592 
593 
594   /* set the end point in the current mask, then create a new empty one */
595   /* (called by "Reset" method)                                         */
596   static FT_Error
ps_dimension_reset_mask(PS_Dimension dim,FT_UInt end_point,FT_Memory memory)597   ps_dimension_reset_mask( PS_Dimension  dim,
598                            FT_UInt       end_point,
599                            FT_Memory     memory )
600   {
601     PS_Mask  mask;
602 
603 
604     /* end current mask */
605     ps_dimension_end_mask( dim, end_point );
606 
607     /* allocate new one */
608     return ps_mask_table_alloc( &dim->masks, memory, &mask );
609   }
610 
611 
612   /* set a new mask, called from the "T2Stem" method */
613   static FT_Error
ps_dimension_set_mask_bits(PS_Dimension dim,const FT_Byte * source,FT_UInt source_pos,FT_UInt source_bits,FT_UInt end_point,FT_Memory memory)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 )
620   {
621     FT_Error  error = 0;
622 
623 
624     /* reset current mask, if any */
625     error = ps_dimension_reset_mask( dim, end_point, memory );
626     if ( error )
627       goto Exit;
628 
629     /* set bits in new mask */
630     error = ps_mask_table_set_bits( &dim->masks, (FT_Byte*)source,
631                                     source_pos, source_bits, memory );
632 
633   Exit:
634     return error;
635   }
636 
637 
638   /* add a new single stem (called from "T1Stem" method) */
639   static FT_Error
ps_dimension_add_t1stem(PS_Dimension dim,FT_Int pos,FT_Int len,FT_Memory memory,FT_Int * aindex)640   ps_dimension_add_t1stem( PS_Dimension  dim,
641                            FT_Int        pos,
642                            FT_Int        len,
643                            FT_Memory     memory,
644                            FT_Int       *aindex )
645   {
646     FT_Error  error = 0;
647     FT_UInt   flags = 0;
648 
649 
650     /* detect ghost stem */
651     if ( len < 0 )
652     {
653       flags |= PS_HINT_FLAG_GHOST;
654       if ( len == -21 )
655       {
656         flags |= PS_HINT_FLAG_BOTTOM;
657         pos   += len;
658       }
659       len = 0;
660     }
661 
662     if ( aindex )
663       *aindex = -1;
664 
665     /* now, lookup stem in the current hints table */
666     {
667       PS_Mask  mask;
668       FT_UInt  idx;
669       FT_UInt  max   = dim->hints.num_hints;
670       PS_Hint  hint  = dim->hints.hints;
671 
672 
673       for ( idx = 0; idx < max; idx++, hint++ )
674       {
675         if ( hint->pos == pos && hint->len == len )
676           break;
677       }
678 
679       /* we need to create a new hint in the table */
680       if ( idx >= max )
681       {
682         error = ps_hint_table_alloc( &dim->hints, memory, &hint );
683         if ( error )
684           goto Exit;
685 
686         hint->pos   = pos;
687         hint->len   = len;
688         hint->flags = flags;
689       }
690 
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;
695 
696       error = ps_mask_set_bit( mask, idx, memory );
697       if ( error )
698         goto Exit;
699 
700       if ( aindex )
701         *aindex = (FT_Int)idx;
702     }
703 
704   Exit:
705     return error;
706   }
707 
708 
709   /* add a "hstem3/vstem3" counter to our dimension table */
710   static FT_Error
ps_dimension_add_counter(PS_Dimension dim,FT_Int hint1,FT_Int hint2,FT_Int hint3,FT_Memory memory)711   ps_dimension_add_counter( PS_Dimension  dim,
712                             FT_Int        hint1,
713                             FT_Int        hint2,
714                             FT_Int        hint3,
715                             FT_Memory     memory )
716   {
717     FT_Error  error   = 0;
718     FT_UInt   count   = dim->counters.num_masks;
719     PS_Mask   counter = dim->counters.masks;
720 
721 
722     /* try to find an existing counter mask that already uses */
723     /* one of these stems here                                */
724     for ( ; count > 0; count--, counter++ )
725     {
726       if ( ps_mask_test_bit( counter, hint1 ) ||
727            ps_mask_test_bit( counter, hint2 ) ||
728            ps_mask_test_bit( counter, hint3 ) )
729         break;
730     }
731 
732     /* creat a new counter when needed */
733     if ( count == 0 )
734     {
735       error = ps_mask_table_alloc( &dim->counters, memory, &counter );
736       if ( error )
737         goto Exit;
738     }
739 
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;
744 
745     error = ps_mask_set_bit( counter, hint2, memory );
746     if ( error )
747       goto Exit;
748 
749     error = ps_mask_set_bit( counter, hint3, memory );
750     if ( error )
751       goto Exit;
752 
753   Exit:
754     return error;
755   }
756 
757 
758   /* end of recording session for a given dimension */
759   static FT_Error
ps_dimension_end(PS_Dimension dim,FT_UInt end_point,FT_Memory memory)760   ps_dimension_end( PS_Dimension  dim,
761                     FT_UInt       end_point,
762                     FT_Memory     memory )
763   {
764     /* end hint mask table */
765     ps_dimension_end_mask( dim, end_point );
766 
767     /* merge all counter masks into independent "paths" */
768     return ps_mask_table_merge_all( &dim->counters, memory );
769   }
770 
771 
772   /*************************************************************************/
773   /*************************************************************************/
774   /*****                                                               *****/
775   /*****                    PS_RECORDER MANAGEMENT                     *****/
776   /*****                                                               *****/
777   /*************************************************************************/
778   /*************************************************************************/
779 
780 
781   /* destroy hints */
782   FT_LOCAL( void )
ps_hints_done(PS_Hints hints)783   ps_hints_done( PS_Hints  hints )
784   {
785     FT_Memory  memory = hints->memory;
786 
787 
788     ps_dimension_done( &hints->dimension[0], memory );
789     ps_dimension_done( &hints->dimension[1], memory );
790 
791     hints->error  = 0;
792     hints->memory = 0;
793   }
794 
795 
796   FT_LOCAL( FT_Error )
ps_hints_init(PS_Hints hints,FT_Memory memory)797   ps_hints_init( PS_Hints   hints,
798                  FT_Memory  memory )
799   {
800     FT_MEM_ZERO( hints, sizeof ( *hints ) );
801     hints->memory = memory;
802     return 0;
803   }
804 
805 
806   /* initialize a hints for a new session */
807   static void
ps_hints_open(PS_Hints hints,PS_Hint_Type hint_type)808   ps_hints_open( PS_Hints      hints,
809                  PS_Hint_Type  hint_type )
810   {
811     switch ( hint_type )
812     {
813     case PS_HINT_TYPE_1:
814     case PS_HINT_TYPE_2:
815       hints->error     = 0;
816       hints->hint_type = hint_type;
817 
818       ps_dimension_init( &hints->dimension[0] );
819       ps_dimension_init( &hints->dimension[1] );
820       break;
821 
822     default:
823       hints->error     = FT_Err_Invalid_Argument;
824       hints->hint_type = hint_type;
825 
826       FT_ERROR(( "ps_hints_open: invalid charstring type!\n" ));
827       break;
828     }
829   }
830 
831 
832   /* add one or more stems to the current hints table */
833   static void
ps_hints_stem(PS_Hints hints,FT_Int dimension,FT_UInt count,FT_Long * stems)834   ps_hints_stem( PS_Hints  hints,
835                  FT_Int    dimension,
836                  FT_UInt   count,
837                  FT_Long*  stems )
838   {
839     if ( !hints->error )
840     {
841       /* limit "dimension" to 0..1 */
842       if ( dimension < 0 || dimension > 1 )
843       {
844         FT_ERROR(( "ps_hints_stem: invalid dimension (%d) used\n",
845                    dimension ));
846         dimension = ( dimension != 0 );
847       }
848 
849       /* record the stems in the current hints/masks table */
850       switch ( hints->hint_type )
851       {
852       case PS_HINT_TYPE_1:  /* Type 1 "hstem" or "vstem" operator */
853       case PS_HINT_TYPE_2:  /* Type 2 "hstem" or "vstem" operator */
854         {
855           PS_Dimension  dim = &hints->dimension[dimension];
856 
857 
858           for ( ; count > 0; count--, stems += 2 )
859           {
860             FT_Error   error;
861             FT_Memory  memory = hints->memory;
862 
863 
864             error = ps_dimension_add_t1stem( dim, stems[0], stems[1],
865                                              memory, NULL );
866             if ( error )
867             {
868               FT_ERROR(( "ps_hints_stem: could not add stem"
869                          " (%d,%d) to hints table\n", stems[0], stems[1] ));
870 
871               hints->error = error;
872               return;
873             }
874           }
875           break;
876         }
877 
878       default:
879         FT_ERROR(( "ps_hints_stem: called with invalid hint type (%d)\n",
880                    hints->hint_type ));
881         break;
882       }
883     }
884   }
885 
886 
887   /* add one Type1 counter stem to the current hints table */
888   static void
ps_hints_t1stem3(PS_Hints hints,FT_Int dimension,FT_Long * stems)889   ps_hints_t1stem3( PS_Hints  hints,
890                     FT_Int    dimension,
891                     FT_Long*  stems )
892   {
893     FT_Error  error = 0;
894 
895 
896     if ( !hints->error )
897     {
898       PS_Dimension  dim;
899       FT_Memory     memory = hints->memory;
900       FT_Int        count;
901       FT_Int        idx[3];
902 
903 
904       /* limit "dimension" to 0..1 */
905       if ( dimension < 0 || dimension > 1 )
906       {
907         FT_ERROR(( "ps_hints_t1stem3: invalid dimension (%d) used\n",
908                    dimension ));
909         dimension = ( dimension != 0 );
910       }
911 
912       dim = &hints->dimension[dimension];
913 
914       /* there must be 6 elements in the 'stem' array */
915       if ( hints->hint_type == PS_HINT_TYPE_1 )
916       {
917         /* add the three stems to our hints/masks table */
918         for ( count = 0; count < 3; count++, stems += 2 )
919         {
920           error = ps_dimension_add_t1stem( dim, stems[0], stems[1],
921                                            memory, &idx[count] );
922           if ( error )
923             goto Fail;
924         }
925 
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;
931       }
932       else
933       {
934         FT_ERROR(( "ps_hints_t1stem3: called with invalid hint type!\n" ));
935         error = FT_Err_Invalid_Argument;
936         goto Fail;
937       }
938     }
939 
940     return;
941 
942   Fail:
943     FT_ERROR(( "ps_hints_t1stem3: could not add counter stems to table\n" ));
944     hints->error = error;
945   }
946 
947 
948   /* reset hints (only with Type 1 hints) */
949   static void
ps_hints_t1reset(PS_Hints hints,FT_UInt end_point)950   ps_hints_t1reset( PS_Hints  hints,
951                     FT_UInt   end_point )
952   {
953     FT_Error  error = 0;
954 
955 
956     if ( !hints->error )
957     {
958       FT_Memory  memory = hints->memory;
959 
960 
961       if ( hints->hint_type == PS_HINT_TYPE_1 )
962       {
963         error = ps_dimension_reset_mask( &hints->dimension[0],
964                                          end_point, memory );
965         if ( error )
966           goto Fail;
967 
968         error = ps_dimension_reset_mask( &hints->dimension[1],
969                                          end_point, memory );
970         if ( error )
971           goto Fail;
972       }
973       else
974       {
975         /* invalid hint type */
976         error = FT_Err_Invalid_Argument;
977         goto Fail;
978       }
979     }
980     return;
981 
982   Fail:
983     hints->error = error;
984   }
985 
986 
987   /* Type2 "hintmask" operator, add a new hintmask to each direction */
988   static void
ps_hints_t2mask(PS_Hints hints,FT_UInt end_point,FT_UInt bit_count,const FT_Byte * bytes)989   ps_hints_t2mask( PS_Hints        hints,
990                    FT_UInt         end_point,
991                    FT_UInt         bit_count,
992                    const FT_Byte*  bytes )
993   {
994     FT_Error  error;
995 
996 
997     if ( !hints->error )
998     {
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;
1003 
1004 
1005       /* check bit count; must be equal to current total hint count */
1006       if ( bit_count !=  count1 + count2 )
1007       {
1008         FT_ERROR(( "ps_hints_t2mask: "
1009                    "called with invalid bitcount %d (instead of %d)\n",
1010                    bit_count, count1 + count2 ));
1011 
1012         /* simply ignore the operator */
1013         return;
1014       }
1015 
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;
1021 
1022       error = ps_dimension_set_mask_bits( &dim[1], bytes, count1, count2,
1023                                           end_point, memory );
1024       if ( error )
1025         goto Fail;
1026     }
1027     return;
1028 
1029   Fail:
1030     hints->error = error;
1031   }
1032 
1033 
1034   static void
ps_hints_t2counter(PS_Hints hints,FT_UInt bit_count,const FT_Byte * bytes)1035   ps_hints_t2counter( PS_Hints        hints,
1036                       FT_UInt         bit_count,
1037                       const FT_Byte*  bytes )
1038   {
1039     FT_Error  error;
1040 
1041 
1042     if ( !hints->error )
1043     {
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;
1048 
1049 
1050       /* check bit count, must be equal to current total hint count */
1051       if ( bit_count !=  count1 + count2 )
1052       {
1053         FT_ERROR(( "ps_hints_t2counter: "
1054                    "called with invalid bitcount %d (instead of %d)\n",
1055                    bit_count, count1 + count2 ));
1056 
1057         /* simply ignore the operator */
1058         return;
1059       }
1060 
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;
1066 
1067       error = ps_dimension_set_mask_bits( &dim[1], bytes, count1, count2,
1068                                           0, memory );
1069       if ( error )
1070         goto Fail;
1071     }
1072     return;
1073 
1074   Fail:
1075     hints->error = error;
1076   }
1077 
1078 
1079   /* end recording session */
1080   static FT_Error
ps_hints_close(PS_Hints hints,FT_UInt end_point)1081   ps_hints_close( PS_Hints  hints,
1082                   FT_UInt   end_point )
1083   {
1084     FT_Error  error;
1085 
1086 
1087     error = hints->error;
1088     if ( !error )
1089     {
1090       FT_Memory     memory = hints->memory;
1091       PS_Dimension  dim    = hints->dimension;
1092 
1093 
1094       error = ps_dimension_end( &dim[0], end_point, memory );
1095       if ( !error )
1096       {
1097         error = ps_dimension_end( &dim[1], end_point, memory );
1098       }
1099     }
1100 
1101 #ifdef DEBUG_HINTER
1102     if ( !error )
1103       ps_debug_hints = hints;
1104 #endif
1105     return error;
1106   }
1107 
1108 
1109   /*************************************************************************/
1110   /*************************************************************************/
1111   /*****                                                               *****/
1112   /*****                TYPE 1 HINTS RECORDING INTERFACE               *****/
1113   /*****                                                               *****/
1114   /*************************************************************************/
1115   /*************************************************************************/
1116 
1117   static void
t1_hints_open(T1_Hints hints)1118   t1_hints_open( T1_Hints  hints )
1119   {
1120     ps_hints_open( (PS_Hints)hints, PS_HINT_TYPE_1 );
1121   }
1122 
1123   static void
t1_hints_stem(T1_Hints hints,FT_Int dimension,FT_Long * coords)1124   t1_hints_stem( T1_Hints  hints,
1125                  FT_Int    dimension,
1126                  FT_Long*  coords )
1127   {
1128     ps_hints_stem( (PS_Hints)hints, dimension, 1, coords );
1129   }
1130 
1131 
1132   FT_LOCAL_DEF( void )
t1_hints_funcs_init(T1_Hints_FuncsRec * funcs)1133   t1_hints_funcs_init( T1_Hints_FuncsRec*  funcs )
1134   {
1135     FT_MEM_ZERO( (char*)funcs, sizeof ( *funcs ) );
1136 
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;
1143   }
1144 
1145 
1146   /*************************************************************************/
1147   /*************************************************************************/
1148   /*****                                                               *****/
1149   /*****                TYPE 2 HINTS RECORDING INTERFACE               *****/
1150   /*****                                                               *****/
1151   /*************************************************************************/
1152   /*************************************************************************/
1153 
1154   static void
t2_hints_open(T2_Hints hints)1155   t2_hints_open( T2_Hints  hints )
1156   {
1157     ps_hints_open( (PS_Hints)hints, PS_HINT_TYPE_2 );
1158   }
1159 
1160 
1161   static void
t2_hints_stems(T2_Hints hints,FT_Int dimension,FT_Int count,FT_Fixed * coords)1162   t2_hints_stems( T2_Hints   hints,
1163                   FT_Int     dimension,
1164                   FT_Int     count,
1165                   FT_Fixed*  coords )
1166   {
1167     FT_Pos  stems[32], y, n, total = count;
1168 
1169 
1170     y = 0;
1171     while ( total > 0 )
1172     {
1173       /* determine number of stems to write */
1174       count = total;
1175       if ( count > 16 )
1176         count = 16;
1177 
1178       /* compute integer stem positions in font units */
1179       for ( n = 0; n < count * 2; n++ )
1180       {
1181         y       += coords[n];
1182         stems[n] = ( y + 0x8000 ) >> 16;
1183       }
1184 
1185       /* compute lengths */
1186       for ( n = 0; n < count * 2; n += 2 )
1187         stems[n + 1] = stems[n + 1] - stems[n];
1188 
1189       /* add them to the current dimension */
1190       ps_hints_stem( (PS_Hints)hints, dimension, count, stems );
1191 
1192       total -= count;
1193     }
1194   }
1195 
1196 
1197   FT_LOCAL_DEF( void )
t2_hints_funcs_init(T2_Hints_FuncsRec * funcs)1198   t2_hints_funcs_init( T2_Hints_FuncsRec*  funcs )
1199   {
1200     FT_MEM_ZERO( funcs, sizeof ( *funcs ) );
1201 
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;
1208   }
1209 
1210 
1211 /* END */
1212