1
/***************************************************************************/
5
/* FreeType PostScript hints recorder (body). */
7
/* Copyright 2001, 2002, 2003, 2004 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. */
16
/***************************************************************************/
20
#include FT_FREETYPE_H
21
#include FT_INTERNAL_OBJECTS_H
22
#include FT_INTERNAL_DEBUG_H
29
#define FT_COMPONENT trace_pshrec
32
PS_Hints ps_debug_hints = 0;
33
int ps_debug_no_horz_hints = 0;
34
int ps_debug_no_vert_hints = 0;
38
/*************************************************************************/
39
/*************************************************************************/
41
/***** PS_HINT MANAGEMENT *****/
43
/*************************************************************************/
44
/*************************************************************************/
46
/* destroy hints table */
48
ps_hint_table_done( PS_Hint_Table table,
51
FT_FREE( table->hints );
57
/* ensure that a table can contain "count" elements */
59
ps_hint_table_ensure( PS_Hint_Table table,
63
FT_UInt old_max = table->max_hints;
64
FT_UInt new_max = count;
68
if ( new_max > old_max )
70
/* try to grow the table */
71
new_max = FT_PAD_CEIL( new_max, 8 );
72
if ( !FT_RENEW_ARRAY( table->hints, old_max, new_max ) )
73
table->max_hints = new_max;
80
ps_hint_table_alloc( PS_Hint_Table table,
89
count = table->num_hints;
92
if ( count >= table->max_hints )
94
error = ps_hint_table_ensure( table, count, memory );
99
hint = table->hints + count - 1;
104
table->num_hints = count;
112
/*************************************************************************/
113
/*************************************************************************/
115
/***** PS_MASK MANAGEMENT *****/
117
/*************************************************************************/
118
/*************************************************************************/
122
ps_mask_done( PS_Mask mask,
125
FT_FREE( mask->bytes );
132
/* ensure that a mask can contain "count" bits */
134
ps_mask_ensure( PS_Mask mask,
138
FT_UInt old_max = ( mask->max_bits + 7 ) >> 3;
139
FT_UInt new_max = ( count + 7 ) >> 3;
143
if ( new_max > old_max )
145
new_max = FT_PAD_CEIL( new_max, 8 );
146
if ( !FT_RENEW_ARRAY( mask->bytes, old_max, new_max ) )
147
mask->max_bits = new_max * 8;
153
/* test a bit value in a given mask */
155
ps_mask_test_bit( PS_Mask mask,
158
if ( (FT_UInt)idx >= mask->num_bits )
161
return mask->bytes[idx >> 3] & ( 0x80 >> ( idx & 7 ) );
165
/* clear a given bit */
167
ps_mask_clear_bit( PS_Mask mask,
173
if ( (FT_UInt)idx >= mask->num_bits )
176
p = mask->bytes + ( idx >> 3 );
177
p[0] = (FT_Byte)( p[0] & ~( 0x80 >> ( idx & 7 ) ) );
181
/* set a given bit, possibly grow the mask */
183
ps_mask_set_bit( PS_Mask mask,
194
if ( (FT_UInt)idx >= mask->num_bits )
196
error = ps_mask_ensure( mask, idx + 1, memory );
200
mask->num_bits = idx + 1;
203
p = mask->bytes + ( idx >> 3 );
204
p[0] = (FT_Byte)( p[0] | ( 0x80 >> ( idx & 7 ) ) );
211
/* destroy mask table */
213
ps_mask_table_done( PS_Mask_Table table,
216
FT_UInt count = table->max_masks;
217
PS_Mask mask = table->masks;
220
for ( ; count > 0; count--, mask++ )
221
ps_mask_done( mask, memory );
223
FT_FREE( table->masks );
224
table->num_masks = 0;
225
table->max_masks = 0;
229
/* ensure that a mask table can contain "count" masks */
231
ps_mask_table_ensure( PS_Mask_Table table,
235
FT_UInt old_max = table->max_masks;
236
FT_UInt new_max = count;
240
if ( new_max > old_max )
242
new_max = FT_PAD_CEIL( new_max, 8 );
243
if ( !FT_RENEW_ARRAY( table->masks, old_max, new_max ) )
244
table->max_masks = new_max;
250
/* allocate a new mask in a table */
252
ps_mask_table_alloc( PS_Mask_Table table,
261
count = table->num_masks;
264
if ( count > table->max_masks )
266
error = ps_mask_table_ensure( table, count, memory );
271
mask = table->masks + count - 1;
274
table->num_masks = count;
282
/* return last hint mask in a table, create one if the table is empty */
284
ps_mask_table_last( PS_Mask_Table table,
293
count = table->num_masks;
296
error = ps_mask_table_alloc( table, memory, &mask );
301
mask = table->masks + count - 1;
309
/* set a new mask to a given bit range */
311
ps_mask_table_set_bits( PS_Mask_Table table,
321
error = ps_mask_table_last( table, memory, &mask );
325
error = ps_mask_ensure( mask, bit_count, memory );
329
mask->num_bits = bit_count;
333
FT_Byte* read = source + ( bit_pos >> 3 );
334
FT_Int rmask = 0x80 >> ( bit_pos & 7 );
335
FT_Byte* write = mask->bytes;
340
for ( ; bit_count > 0; bit_count-- )
342
val = write[0] & ~wmask;
344
if ( read[0] & rmask )
347
write[0] = (FT_Byte)val;
370
/* test whether two masks in a table intersect */
372
ps_mask_table_test_intersect( PS_Mask_Table table,
376
PS_Mask mask1 = table->masks + index1;
377
PS_Mask mask2 = table->masks + index2;
378
FT_Byte* p1 = mask1->bytes;
379
FT_Byte* p2 = mask2->bytes;
380
FT_UInt count1 = mask1->num_bits;
381
FT_UInt count2 = mask2->num_bits;
385
count = ( count1 <= count2 ) ? count1 : count2;
386
for ( ; count >= 8; count -= 8 )
398
return ( p1[0] & p2[0] ) & ~( 0xFF >> count );
402
/* merge two masks, used by ps_mask_table_merge_all */
404
ps_mask_table_merge( PS_Mask_Table table,
413
/* swap index1 and index2 so that index1 < index2 */
414
if ( index1 > index2 )
421
if ( index1 < index2 && index1 >= 0 && index2 < (FT_Int)table->num_masks )
423
/* we need to merge the bitsets of index1 and index2 with a */
425
PS_Mask mask1 = table->masks + index1;
426
PS_Mask mask2 = table->masks + index2;
427
FT_UInt count1 = mask1->num_bits;
428
FT_UInt count2 = mask2->num_bits;
439
/* if "count2" is greater than "count1", we need to grow the */
440
/* first bitset, and clear the highest bits */
441
if ( count2 > count1 )
443
error = ps_mask_ensure( mask1, count2, memory );
447
for ( pos = count1; pos < count2; pos++ )
448
ps_mask_clear_bit( mask1, pos );
451
/* merge (unite) the bitsets */
453
write = mask1->bytes;
454
pos = (FT_UInt)( ( count2 + 7 ) >> 3 );
456
for ( ; pos > 0; pos-- )
458
write[0] = (FT_Byte)( write[0] | read[0] );
464
/* Now, remove "mask2" from the list. We need to keep the masks */
465
/* sorted in order of importance, so move table elements. */
467
mask2->end_point = 0;
469
delta = table->num_masks - 1 - index2; /* number of masks to move */
472
/* move to end of table for reuse */
473
PS_MaskRec dummy = *mask2;
476
ft_memmove( mask2, mask2 + 1, delta * sizeof ( PS_MaskRec ) );
478
mask2[delta] = dummy;
484
FT_ERROR(( "ps_mask_table_merge: ignoring invalid indices (%d,%d)\n",
492
/* Try to merge all masks in a given table. This is used to merge */
493
/* all counter masks into independent counter "paths". */
496
ps_mask_table_merge_all( PS_Mask_Table table,
499
FT_Int index1, index2;
503
for ( index1 = table->num_masks - 1; index1 > 0; index1-- )
505
for ( index2 = index1 - 1; index2 >= 0; index2-- )
507
if ( ps_mask_table_test_intersect( table, index1, index2 ) )
509
error = ps_mask_table_merge( table, index2, index1, memory );
523
/*************************************************************************/
524
/*************************************************************************/
526
/***** PS_DIMENSION MANAGEMENT *****/
528
/*************************************************************************/
529
/*************************************************************************/
532
/* finalize a given dimension */
534
ps_dimension_done( PS_Dimension dimension,
537
ps_mask_table_done( &dimension->counters, memory );
538
ps_mask_table_done( &dimension->masks, memory );
539
ps_hint_table_done( &dimension->hints, memory );
543
/* initialize a given dimension */
545
ps_dimension_init( PS_Dimension dimension )
547
dimension->hints.num_hints = 0;
548
dimension->masks.num_masks = 0;
549
dimension->counters.num_masks = 0;
555
/* set a bit at a given index in the current hint mask */
557
ps_dimension_set_mask_bit( PS_Dimension dim,
565
/* get last hint mask */
566
error = ps_mask_table_last( &dim->masks, memory, &mask );
570
error = ps_mask_set_bit( mask, idx, memory );
578
/* set the end point in a mask, called from "End" & "Reset" methods */
580
ps_dimension_end_mask( PS_Dimension dim,
583
FT_UInt count = dim->masks.num_masks;
589
mask = dim->masks.masks + count - 1;
590
mask->end_point = end_point;
595
/* set the end point in the current mask, then create a new empty one */
596
/* (called by "Reset" method) */
598
ps_dimension_reset_mask( PS_Dimension dim,
605
/* end current mask */
606
ps_dimension_end_mask( dim, end_point );
608
/* allocate new one */
609
return ps_mask_table_alloc( &dim->masks, memory, &mask );
613
/* set a new mask, called from the "T2Stem" method */
615
ps_dimension_set_mask_bits( PS_Dimension dim,
616
const FT_Byte* source,
625
/* reset current mask, if any */
626
error = ps_dimension_reset_mask( dim, end_point, memory );
630
/* set bits in new mask */
631
error = ps_mask_table_set_bits( &dim->masks, (FT_Byte*)source,
632
source_pos, source_bits, memory );
639
/* add a new single stem (called from "T1Stem" method) */
641
ps_dimension_add_t1stem( PS_Dimension dim,
651
/* detect ghost stem */
654
flags |= PS_HINT_FLAG_GHOST;
657
flags |= PS_HINT_FLAG_BOTTOM;
666
/* now, lookup stem in the current hints table */
670
FT_UInt max = dim->hints.num_hints;
671
PS_Hint hint = dim->hints.hints;
674
for ( idx = 0; idx < max; idx++, hint++ )
676
if ( hint->pos == pos && hint->len == len )
680
/* we need to create a new hint in the table */
683
error = ps_hint_table_alloc( &dim->hints, memory, &hint );
692
/* now, store the hint in the current mask */
693
error = ps_mask_table_last( &dim->masks, memory, &mask );
697
error = ps_mask_set_bit( mask, idx, memory );
702
*aindex = (FT_Int)idx;
710
/* add a "hstem3/vstem3" counter to our dimension table */
712
ps_dimension_add_counter( PS_Dimension dim,
719
FT_UInt count = dim->counters.num_masks;
720
PS_Mask counter = dim->counters.masks;
723
/* try to find an existing counter mask that already uses */
724
/* one of these stems here */
725
for ( ; count > 0; count--, counter++ )
727
if ( ps_mask_test_bit( counter, hint1 ) ||
728
ps_mask_test_bit( counter, hint2 ) ||
729
ps_mask_test_bit( counter, hint3 ) )
733
/* creat a new counter when needed */
736
error = ps_mask_table_alloc( &dim->counters, memory, &counter );
741
/* now, set the bits for our hints in the counter mask */
742
error = ps_mask_set_bit( counter, hint1, memory );
746
error = ps_mask_set_bit( counter, hint2, memory );
750
error = ps_mask_set_bit( counter, hint3, memory );
759
/* end of recording session for a given dimension */
761
ps_dimension_end( PS_Dimension dim,
765
/* end hint mask table */
766
ps_dimension_end_mask( dim, end_point );
768
/* merge all counter masks into independent "paths" */
769
return ps_mask_table_merge_all( &dim->counters, memory );
773
/*************************************************************************/
774
/*************************************************************************/
776
/***** PS_RECORDER MANAGEMENT *****/
778
/*************************************************************************/
779
/*************************************************************************/
784
ps_hints_done( PS_Hints hints )
786
FT_Memory memory = hints->memory;
789
ps_dimension_done( &hints->dimension[0], memory );
790
ps_dimension_done( &hints->dimension[1], memory );
798
ps_hints_init( PS_Hints hints,
801
FT_MEM_ZERO( hints, sizeof ( *hints ) );
802
hints->memory = memory;
807
/* initialize a hints for a new session */
809
ps_hints_open( PS_Hints hints,
810
PS_Hint_Type hint_type )
817
hints->hint_type = hint_type;
819
ps_dimension_init( &hints->dimension[0] );
820
ps_dimension_init( &hints->dimension[1] );
824
hints->error = PSH_Err_Invalid_Argument;
825
hints->hint_type = hint_type;
827
FT_ERROR(( "ps_hints_open: invalid charstring type!\n" ));
833
/* add one or more stems to the current hints table */
835
ps_hints_stem( PS_Hints hints,
842
/* limit "dimension" to 0..1 */
843
if ( dimension < 0 || dimension > 1 )
845
FT_ERROR(( "ps_hints_stem: invalid dimension (%d) used\n",
847
dimension = ( dimension != 0 );
850
/* record the stems in the current hints/masks table */
851
switch ( hints->hint_type )
853
case PS_HINT_TYPE_1: /* Type 1 "hstem" or "vstem" operator */
854
case PS_HINT_TYPE_2: /* Type 2 "hstem" or "vstem" operator */
856
PS_Dimension dim = &hints->dimension[dimension];
859
for ( ; count > 0; count--, stems += 2 )
862
FT_Memory memory = hints->memory;
865
error = ps_dimension_add_t1stem(
866
dim, (FT_Int)stems[0], (FT_Int)stems[1],
870
FT_ERROR(( "ps_hints_stem: could not add stem"
871
" (%d,%d) to hints table\n", stems[0], stems[1] ));
873
hints->error = error;
881
FT_ERROR(( "ps_hints_stem: called with invalid hint type (%d)\n",
889
/* add one Type1 counter stem to the current hints table */
891
ps_hints_t1stem3( PS_Hints hints,
901
FT_Memory memory = hints->memory;
906
/* limit "dimension" to 0..1 */
907
if ( dimension < 0 || dimension > 1 )
909
FT_ERROR(( "ps_hints_t1stem3: invalid dimension (%d) used\n",
911
dimension = ( dimension != 0 );
914
dim = &hints->dimension[dimension];
916
/* there must be 6 elements in the 'stem' array */
917
if ( hints->hint_type == PS_HINT_TYPE_1 )
919
/* add the three stems to our hints/masks table */
920
for ( count = 0; count < 3; count++, stems += 2 )
922
error = ps_dimension_add_t1stem(
923
dim, (FT_Int)stems[0], (FT_Int)stems[1],
924
memory, &idx[count] );
929
/* now, add the hints to the counters table */
930
error = ps_dimension_add_counter( dim, idx[0], idx[1], idx[2],
937
FT_ERROR(( "ps_hints_t1stem3: called with invalid hint type!\n" ));
938
error = PSH_Err_Invalid_Argument;
946
FT_ERROR(( "ps_hints_t1stem3: could not add counter stems to table\n" ));
947
hints->error = error;
951
/* reset hints (only with Type 1 hints) */
953
ps_hints_t1reset( PS_Hints hints,
961
FT_Memory memory = hints->memory;
964
if ( hints->hint_type == PS_HINT_TYPE_1 )
966
error = ps_dimension_reset_mask( &hints->dimension[0],
971
error = ps_dimension_reset_mask( &hints->dimension[1],
978
/* invalid hint type */
979
error = PSH_Err_Invalid_Argument;
986
hints->error = error;
990
/* Type2 "hintmask" operator, add a new hintmask to each direction */
992
ps_hints_t2mask( PS_Hints hints,
995
const FT_Byte* bytes )
1000
if ( !hints->error )
1002
PS_Dimension dim = hints->dimension;
1003
FT_Memory memory = hints->memory;
1004
FT_UInt count1 = dim[0].hints.num_hints;
1005
FT_UInt count2 = dim[1].hints.num_hints;
1008
/* check bit count; must be equal to current total hint count */
1009
if ( bit_count != count1 + count2 )
1011
FT_ERROR(( "ps_hints_t2mask: "
1012
"called with invalid bitcount %d (instead of %d)\n",
1013
bit_count, count1 + count2 ));
1015
/* simply ignore the operator */
1019
/* set-up new horizontal and vertical hint mask now */
1020
error = ps_dimension_set_mask_bits( &dim[0], bytes, count2, count1,
1021
end_point, memory );
1025
error = ps_dimension_set_mask_bits( &dim[1], bytes, 0, count2,
1026
end_point, memory );
1033
hints->error = error;
1038
ps_hints_t2counter( PS_Hints hints,
1040
const FT_Byte* bytes )
1045
if ( !hints->error )
1047
PS_Dimension dim = hints->dimension;
1048
FT_Memory memory = hints->memory;
1049
FT_UInt count1 = dim[0].hints.num_hints;
1050
FT_UInt count2 = dim[1].hints.num_hints;
1053
/* check bit count, must be equal to current total hint count */
1054
if ( bit_count != count1 + count2 )
1056
FT_ERROR(( "ps_hints_t2counter: "
1057
"called with invalid bitcount %d (instead of %d)\n",
1058
bit_count, count1 + count2 ));
1060
/* simply ignore the operator */
1064
/* set-up new horizontal and vertical hint mask now */
1065
error = ps_dimension_set_mask_bits( &dim[0], bytes, 0, count1,
1070
error = ps_dimension_set_mask_bits( &dim[1], bytes, count1, count2,
1078
hints->error = error;
1082
/* end recording session */
1084
ps_hints_close( PS_Hints hints,
1090
error = hints->error;
1093
FT_Memory memory = hints->memory;
1094
PS_Dimension dim = hints->dimension;
1097
error = ps_dimension_end( &dim[0], end_point, memory );
1100
error = ps_dimension_end( &dim[1], end_point, memory );
1106
ps_debug_hints = hints;
1112
/*************************************************************************/
1113
/*************************************************************************/
1115
/***** TYPE 1 HINTS RECORDING INTERFACE *****/
1117
/*************************************************************************/
1118
/*************************************************************************/
1121
t1_hints_open( T1_Hints hints )
1123
ps_hints_open( (PS_Hints)hints, PS_HINT_TYPE_1 );
1127
t1_hints_stem( T1_Hints hints,
1131
ps_hints_stem( (PS_Hints)hints, dimension, 1, coords );
1135
FT_LOCAL_DEF( void )
1136
t1_hints_funcs_init( T1_Hints_FuncsRec* funcs )
1138
FT_MEM_ZERO( (char*)funcs, sizeof ( *funcs ) );
1140
funcs->open = (T1_Hints_OpenFunc) t1_hints_open;
1141
funcs->close = (T1_Hints_CloseFunc) ps_hints_close;
1142
funcs->stem = (T1_Hints_SetStemFunc) t1_hints_stem;
1143
funcs->stem3 = (T1_Hints_SetStem3Func)ps_hints_t1stem3;
1144
funcs->reset = (T1_Hints_ResetFunc) ps_hints_t1reset;
1145
funcs->apply = (T1_Hints_ApplyFunc) ps_hints_apply;
1149
/*************************************************************************/
1150
/*************************************************************************/
1152
/***** TYPE 2 HINTS RECORDING INTERFACE *****/
1154
/*************************************************************************/
1155
/*************************************************************************/
1158
t2_hints_open( T2_Hints hints )
1160
ps_hints_open( (PS_Hints)hints, PS_HINT_TYPE_2 );
1165
t2_hints_stems( T2_Hints hints,
1170
FT_Pos stems[32], y, n;
1171
FT_Int total = count;
1177
/* determine number of stems to write */
1182
/* compute integer stem positions in font units */
1183
for ( n = 0; n < count * 2; n++ )
1186
stems[n] = ( y + 0x8000L ) >> 16;
1189
/* compute lengths */
1190
for ( n = 0; n < count * 2; n += 2 )
1191
stems[n + 1] = stems[n + 1] - stems[n];
1193
/* add them to the current dimension */
1194
ps_hints_stem( (PS_Hints)hints, dimension, count, stems );
1201
FT_LOCAL_DEF( void )
1202
t2_hints_funcs_init( T2_Hints_FuncsRec* funcs )
1204
FT_MEM_ZERO( funcs, sizeof ( *funcs ) );
1206
funcs->open = (T2_Hints_OpenFunc) t2_hints_open;
1207
funcs->close = (T2_Hints_CloseFunc) ps_hints_close;
1208
funcs->stems = (T2_Hints_StemsFunc) t2_hints_stems;
1209
funcs->hintmask= (T2_Hints_MaskFunc) ps_hints_t2mask;
1210
funcs->counter = (T2_Hints_CounterFunc)ps_hints_t2counter;
1211
funcs->apply = (T2_Hints_ApplyFunc) ps_hints_apply;