summaryrefslogtreecommitdiff
path: root/src/pshinter/pshfit.c
blob: 1f9a70bf076a36aef0bf30e72e5c650b3d8aef05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
#include <ft2build.h>
#include FT_INTERNAL_OBJECTS_H
#include FT_INTERNAL_DEBUG_H
#include "pshfit.h"
#include "pshoptim.h"
 
 /* return true iff two stem hints overlap */
  static FT_Int
  psh_hint_overlap( PSH_Hint  hint1,
                    PSH_Hint  hint2 )
  {
    return ( hint1->org_pos + hint1->org_len >= hint2->org_pos &&
             hint2->org_pos + hint2->org_len >= hint1->org_pos );
  }
 
 
 /* destroy hints table */
  static void
  psh_hint_table_done( PSH_Hint_Table  table,
                       FT_Memory       memory )
  {
    FREE( table->zones );
    table->num_zones = 0;
    table->zone      = 0;
    
    FREE( table->sort );
    FREE( table->hints );
    table->num_hints   = 0;
    table->max_hints   = 0;
    table->sort_global = 0;
  }


 /* deactivate all hints in a table */
  static void
  psh_hint_table_deactivate( PSH_Hint_Table  table )
  {
    FT_UInt   count = table->max_hints;
    PSH_Hint  hint  = table->hints;
    
    for ( ; count > 0; count--, hint++ )
    {
      psh_hint_deactivate(hint);
      hint->order = -1;
    }
  }


 /* internal function used to record a new hint */
  static void
  psh_hint_table_record( PSH_Hint_Table  table,
                         FT_UInt         index )
  {
    PSH_Hint  hint = table->hints + index;

    if ( index >= table->max_hints )
    {
      FT_ERROR(( "%s.activate: invalid hint index %d\n", index ));
      return;
    }
        
    /* ignore active hints */
    if ( psh_hint_is_active(hint) )
      return;
    
    psh_hint_activate(hint);
    
    /* now scan the current active hint set in order to determine */
    /* if we're overlapping with another segment..                */
    {
      PSH_Hint*  sorted = table->sort_global;
      FT_UInt    count  = table->num_hints;
      PSH_Hint   hint2;

      hint->parent = 0;      
      for ( ; count > 0; count--, sorted++ )
      {
        hint2 = sorted[0];
        
        if ( psh_hint_overlap( hint, hint2 ) )
        {
          hint->parent = hint2;
          break;
        }
      }
    }
    
    if ( table->num_hints < table->max_hints )
      table->sort_global[ table->num_hints++ ] = hint;
    else
    {
      FT_ERROR(( "%s.activate: too many sorted hints !! BUG !!\n",
                 "ps.fitter" ));
    }
  }


  static void
  psh_hint_table_record_mask( PSH_Hint_Table  table,
                              PS_Mask         hint_mask )
  {
    FT_Int    mask = 0, val = 0;
    FT_Byte*  cursor = hint_mask->bytes;
    FT_UInt   index, limit;

    limit = hint_mask->num_bits; 
    
    if ( limit != table->max_hints )
    {
      FT_ERROR(( "%s.activate_mask: invalid bit count (%d instead of %d)\n",
                 "ps.fitter", hint_mask->num_bits, table->max_hints ));
    }
        
    for ( index = 0; index < limit; index++ )
    {
      if ( mask == 0 )
      {
        val  = *cursor++;
        mask = 0x80;
      }
      
      if ( val & mask )
        psh_hint_table_record( table, index );
        
      mask >>= 1;
    }
  }


 /* create hints table */
  static FT_Error
  psh_hint_table_init( PSH_Hint_Table  table,
                       PS_Hint_Table   hints,
                       PS_Mask_Table   hint_masks,
                       PS_Mask_Table   counter_masks,
                       FT_Memory       memory )
  {
    FT_UInt   count = hints->num_hints;
    FT_Error  error;

    FT_UNUSED(counter_masks);
    
    /* allocate our tables */
    if ( ALLOC_ARRAY( table->sort,  2*count,   PSH_Hint    ) ||
         ALLOC_ARRAY( table->hints,   count,   PSH_HintRec ) ||
         ALLOC_ARRAY( table->zones, 2*count+1, PSH_ZoneRec ) )
      goto Exit;
    
    table->max_hints   = count;
    table->sort_global = table->sort + count;
    table->num_hints   = 0;
    table->num_zones   = 0;
    table->zone        = 0;
    
    /* now, initialise the "hints" array */
    {
      PSH_Hint  write = table->hints;
      PS_Hint   read  = hints->hints;
      
      for ( ; count > 0; count--, write++, read++ )
      {
        write->org_pos = read->pos;
        write->org_len = read->len;
        write->flags   = read->flags;
      }
    }

    /* we now need to determine the initial "parent" stems, first  */
    /* activate the hints that are given by the initial hint masks */
    if ( hint_masks )
    {
      FT_UInt  count = hint_masks->num_masks;
      PS_Mask  mask  = hint_masks->masks;

      table->hint_masks = hint_masks;
      
      for ( ; count > 0; count--, mask++ )
        psh_hint_table_record_mask( table, mask );
    }
    
    /* now, do a linear parse in case some hints were left alone */
    if ( table->num_hints != table->max_hints )
    {
      FT_UInt   index, count;
      
      FT_ERROR(( "%s.init: missing/incorrect hint masks !!\n" ));
      count = table->max_hints;
      for ( index = 0; index < count; index++ )
        psh_hint_table_record( table, index );
    }    
    
  Exit:
    return error;
  }



  static void
  psh_hint_table_activate_mask( PSH_Hint_Table  table,
                                PS_Mask         hint_mask )
  {
    FT_Int    mask = 0, val = 0;
    FT_Byte*  cursor = hint_mask->bytes;
    FT_UInt   index, limit, count;

    limit = hint_mask->num_bits; 
    count = 0;

    psh_hint_table_deactivate( table );
    
    for ( index = 0; index < limit; index++ )
    {
      if ( mask == 0 )
      {
        val  = *cursor++;
        mask = 0x80;
      }
      
      if ( val & mask )
      {
        PSH_Hint  hint = &table->hints[index];
        
        if ( !psh_hint_is_active(hint) )
        {
          PSH_Hint*  sort   = table->sort;
          FT_UInt    count2;
          PSH_Hint   hint2;
          
          for ( count2 = count; count2 > 0; count2--, sort++ )
          {
            hint2 = sort[0];
            if ( psh_hint_overlap( hint, hint2 ) )
            {
              FT_ERROR(( "%s.activate_mask: found overlapping hints\n",
                         "psf.hint" ));
              break;
            }
          }
          
          if ( count2 == 0 )
          {
            psh_hint_activate( hint );
            if ( count < table->max_hints )
              table->sort[count++] = hint;
            else
            {
              FT_ERROR(( "%s.activate_mask: too many active hints\n",
                         "psf.hint" ));
            } 
          }
        }
      }
        
      mask >>= 1;
    }
    table->num_hints = count;
    
    /* now, sort the hints, they're guaranteed to not overlap */
    /* so we can compare their "org_pos" field directly..     */
    {
      FT_Int     i1, i2;
      PSH_Hint   hint1, hint2;
      PSH_Hint*  sort = table->sort;

      /* a simple bubble sort will do, since in 99% of cases, the hints */
      /* will be already sorted.. and the sort will be linear           */
      for ( i1 = 1; i1 < (FT_Int)count; i1++ )
      {
        hint1 = sort[i1];
        for ( i2 = i1-1; i2 >= 0; i2-- )
        {
          hint2 = sort[i2];
          if ( hint2->org_pos < hint1->org_pos )
            break;
            
          sort[i1] = hint2;
          sort[i2] = hint1;
        }
      }
    }
  }


#define  PSH_ZONE_MIN  -3200000
#define  PSH_ZONE_MAX  +3200000


#define xxDEBUG_ZONES

#ifdef DEBUG_ZONES

#include <stdio.h>

  static void
  print_zone( PSH_Zone  zone )
  {
    printf( "zone [scale,delta,min,max] = [%.3f,%.3f,%d,%d]\n",
             zone->scale/65536.0,
             zone->delta/64.0,
             zone->min,
             zone->max );
  }

#else
#  define  print_zone(x)   do { } while (0)
#endif

 /* setup interpolation zones once the hints have been grid-fitted */
 /* by the optimizer..                                             */
  static void
  psh_hint_table_setup_zones( PSH_Hint_Table  table,
                              FT_Fixed        scale,
                              FT_Fixed        delta )
  {
    FT_UInt   count;
    PSH_Zone  zone;
    PSH_Hint *sort, hint, hint2;
    
    zone  = table->zones;
    
    /* special case, no hints defined */
    if ( table->num_hints == 0 )
    {
      zone->scale = scale;
      zone->delta = delta;
      zone->min   = PSH_ZONE_MIN;
      zone->max   = PSH_ZONE_MAX;
      table->num_zones = 1;
      return;
    }
    
    /* the first zone is before the first hint */
    /* x' = (x-x0)*s + x0' = x*s + ( x0' - x0*s ) */
    sort  = table->sort;
    hint  = sort[0];
    
    zone->scale = scale;
    zone->delta = hint->cur_pos - FT_MulFix( hint->org_pos, scale );
    zone->min   = PSH_ZONE_MIN;
    zone->max   = hint->org_pos;
    
    print_zone( zone );
    
    zone++;
    
    for ( count = table->num_hints; count > 0; count-- )
    {
      FT_Fixed  scale2;

      if ( hint->org_len > 0 )
      {
        /* setup a zone for inner-stem interpolation */
        /* (x' - x0') = (x - x0)*(x1'-x0')/(x1-x0)   */
        /* x' = x*s2 + x0' - x0*s2                   */
        
        scale2      = FT_DivFix( hint->cur_len, hint->org_len );
        zone->scale = scale2;
        zone->min   = hint->org_pos;
        zone->max   = hint->org_pos + hint->org_len;
        zone->delta = hint->cur_pos - FT_MulFix( zone->min, scale2 );

        print_zone( zone );
    
        zone++;
      }
      
      if ( count == 1 )
        break;
        
      sort++;
      hint2 = sort[0];
      
      /* setup zone for inter-stem interpolation */
      /* (x'-x1') = (x-x1)*(x2'-x1')/(x2-x1)     */
      /* x' = x*s3 + x1' - x1*s3                 */
      scale2 = FT_DivFix( hint2->cur_pos - (hint->cur_pos + hint->cur_len),
                          hint2->org_pos - (hint->org_pos + hint->org_len) );
      zone->scale = scale2;
      zone->min   = hint->org_pos + hint->org_len;
      zone->max   = hint2->org_pos;
      zone->delta = hint->cur_pos + hint->cur_len - FT_MulFix( zone->min, scale2 );

      print_zone( zone );
    
      zone++;
      
      hint  = hint2;
    }

    /* the last zone */
    zone->scale = scale;
    zone->min   = hint->org_pos + hint->org_len;
    zone->max   = PSH_ZONE_MAX;
    zone->delta = hint->cur_pos + hint->cur_len - FT_MulFix( zone->min, scale );

    print_zone( zone );
    
    zone++;
    
    table->num_zones = zone - table->zones;
    table->zone      = table->zones;
  }


 /* tune a single coordinate with the current interpolation zones */  
  static FT_Pos
  psh_hint_table_tune_coord( PSH_Hint_Table  table,
                             FT_Int          coord )
  {
    PSH_Zone   zone;
    
    zone = table->zone;
      
    if ( coord < zone->min )
    {
      do
      {
        if ( zone == table->zones )
          break;
          
        zone--;
      }
      while ( coord < zone->min );
      table->zone = zone;
    }
    else if ( coord > zone->max )
    {
      do
      {
        if ( zone == table->zones + table->num_zones - 1 )
          break;
          
        zone++;
      }
      while ( coord > zone->max );
      table->zone = zone;
    }
        
    return FT_MulFix( coord, zone->scale ) + zone->delta;
  }


 /* tune a given outline with current interpolation zones */
 /* the function only works in a single dimension..       */
  static void
  psh_hint_table_tune_outline( PSH_Hint_Table  table,
                               FT_Outline*     outline,
                               PSH_Globals     globals,
                               FT_Bool         vertical )

  {
    FT_UInt        count, first, last;
    PS_Mask_Table  hint_masks = table->hint_masks;
    PS_Mask        mask;
    PSH_Dimension  dim        = &globals->dimension[vertical];
    FT_Fixed       scale      = dim->scale_mult;
    FT_Fixed       delta      = dim->scale_delta;
    
    if ( hint_masks && hint_masks->num_masks > 0 )
    {
      first = 0;
      mask  = hint_masks->masks;
      count = hint_masks->num_masks;
      for ( ; count > 0; count--, mask++ )
      {
        last = mask->end_point;
        
        if ( last > first )
        {
          FT_Vector*   vec;
          FT_Int       count2;
          
          psh_hint_table_activate_mask( table, mask );
          psh_hint_table_optimize( table, globals, outline, vertical );
          psh_hint_table_setup_zones( table, scale, delta );
          last = mask->end_point;
          
          vec    = outline->points + first;
          count2 = last - first;
          for ( ; count2 > 0; count2--, vec++ )
          {
            FT_Pos  x, *px;
            
            px  = vertical ? &vec->x : &vec->y;
            x   = *px;
            
            *px = psh_hint_table_tune_coord( table, (FT_Int)x );
          }
        }
          
        first = last;
      }
    }
    else    /* no hints in this glyph, simply scale the outline */
    {
      FT_Vector*  vec;
      
      vec   = outline->points;
      count = outline->n_points;
      
      if ( vertical )
      {
        for ( ; count > 0; count--, vec++ )
          vec->x = FT_MulFix( vec->x, scale ) + delta;
      }
      else
      {
        for ( ; count > 0; count--, vec++ )
          vec->y = FT_MulFix( vec->y, scale ) + delta;
      }
    }
  }
  
  
  


  
  
  
  
  
  FT_LOCAL_DEF FT_Error
  ps_hints_apply( PS_Hints     ps_hints,
                  FT_Outline*  outline,
                  PSH_Globals  globals )
  {
    PSH_Hint_TableRec  hints;
    FT_Error           error;
    FT_Int             dimension;
    
    for ( dimension = 1; dimension >= 0; dimension-- )
    {
      PS_Dimension  dim = &ps_hints->dimension[dimension];
      
      /* initialise hints table */
      memset( &hints, 0, sizeof(hints) );
      error = psh_hint_table_init( &hints,
                                   &dim->hints,
                                   &dim->masks,
                                   &dim->counters,
                                   ps_hints->memory );
      if (error) goto Exit;
      
      psh_hint_table_tune_outline( &hints,
                                   outline,
                                   globals,
                                   dimension );
                                   
      psh_hint_table_done( &hints, ps_hints->memory );
    }
    
  Exit:
    return error;                                   
  }