summaryrefslogtreecommitdiff
path: root/src/pshinter/pshglob.c
blob: 480116db44ebf0775fb265ba3c7d6d37ef24c862 (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
#include <ft2build.h>
#include "pshglob.h"

/* "simple" ps hinter globals management, inspired from the new auto-hinter */

 /*************************************************************************/
 /*************************************************************************/
 /*****                                                               *****/
 /*****                       STANDARD WIDTHS                         *****/
 /*****                                                               *****/
 /*************************************************************************/
 /*************************************************************************/
 

 /* reset the widths/heights table */
  static void
  psh_globals_reset_widths( PSH_Globals        globals,
                            FT_UInt            direction,
                            PS_Globals_Widths  widths )
  {
    PSH_Dimension  dim    = &globals->dimension[direction];
    
    /* simple copy of the original widths values - no sorting */
    {
      FT_UInt    count = widths->count;
      PSH_Width  write = dim->std.widths;
      FT_Int16*  read  = widths->widths;

      dim->std.count = count;
      for ( ; count > 0; count-- )
      {
        write->org = read[0];
        write++;
        read++;
      }
    }
  }


 /* scale the widths/heights table */
  static void
  psh_globals_scale_widths( PSH_Globals   globals,
                            FT_UInt       direction )
  {
    PSH_Dimension  dim   = &globals->dimension[direction];
    PSH_Widths     std   = &dim->std;
    FT_UInt        count = std->count;
    PSH_Width      width = std->widths;
    FT_Fixed       scale = dim->scale_mult;

    for ( ; count > 0; count--, width++ )
    {
      width->cur = FT_MulFix( width->org, scale );
      width->fit = FT_RoundFix( width->cur );
    }
  }

 

  FT_LOCAL_DEF FT_Pos
  psh_dimension_snap_width( PSH_Dimension  dimension,
                            FT_Int         org_width )
  {
    FT_UInt  n;
    FT_Pos   width     = FT_MulFix( org_width, dimension->scale_mult );
    FT_Pos   best      = 64 + 32 + 2;
    FT_Pos   reference = width;

    for ( n = 0; n < dimension->std.count; n++ )
    {
      FT_Pos  w;
      FT_Pos  dist;

      w = dimension->std.widths[n].cur;
      dist = width - w;
      if ( dist < 0 )
        dist = -dist;
      if ( dist < best )
      {
        best      = dist;
        reference = w;
      }
    }

    if ( width >= reference )
    {
      width -= 0x21;
      if ( width < reference )
        width = reference;
    }
    else
    {
      width += 0x21;
      if ( width > reference )
        width = reference;
    }

    return width;
  }
  

 /*************************************************************************/
 /*************************************************************************/
 /*****                                                               *****/
 /*****                       BLUE ZONES                              *****/
 /*****                                                               *****/
 /*************************************************************************/
 /*************************************************************************/
  
  static void
  psh_blues_reset_zones( PSH_Blues         target,
                         PS_Globals_Blues  source,
                         FT_Int            family )
  {
    PSH_Blue_Table  top_table, bot_table;
    FT_Int16*       read;
    FT_Int          read_count, count, count_top, count_bot;
    
    if ( family )
    {
      top_table  = &target->family_top;
      bot_table  = &target->family_bottom;
      read       = source->zones_family;
      read_count = (FT_Int)source->count_family;
    }
    else
    {
      top_table  = &target->normal_top;
      bot_table  = &target->normal_bottom;
      read       = source->zones;
      read_count = (FT_Int)source->count;
    }
    
    /* read the input blue zones, and build two sorted tables */
    /* (one for the top zones, the other for the bottom zones */
    count_top = 0;
    count_bot = 0;
    for ( ; read_count > 0; read_count-- )
    {
      FT_Int         reference, delta;
      PSH_Blue_Zone  zones, zone;
      
      /* read blue zone entry, and select target top/bottom zone */
      reference = read[0];
      delta     = read[1] - reference;
      
      if ( delta >= 0 )
      {
        zones = top_table->zones;
        count = count_top;
      }
      else
      {
        zones = bot_table->zones;
        count = count_bot;
      }
      
      /* insert into sorted table */
      zone = zones;  
      for ( ; count > 0; count--, zone++ )
      {
        if ( reference < zone->org_ref )
          break;
          
        if ( reference == zone->org_ref )
        {
          FT_Int  delta0 = zone->org_delta;

          /* we have two zones on the same reference position */
          /* only keep the largest one..                      */          
          if ( delta < 0 )
          {
            if ( delta < delta0 )
              zone->org_delta = delta;
          }
          else
          {
            if ( delta > delta0 )
              zone->org_delta = delta;
          }
          goto Skip;
        }
      }
      
      for ( ; count > 0; count-- )
        zone[count] = zone[count-1];
        
      zone->org_ref   = reference;
      zone->org_delta = delta;
      
      if ( delta >= 0 )
        count_top ++;
      else
        count_bot ++;
      
    Skip:
      read += 2;
    }    

    top_table->count = count_top;
    bot_table->count = count_bot;

    /* sanitize top table */
    {
      PSH_Blue_Zone  zone = top_table->zones;
      
      for ( count = count_top-1; count > 0; count--, zone++ )
      {
        FT_Int  delta;
      
        delta = zone[1].org_ref - zone[0].org_ref;
        if ( zone->org_delta > delta )
          zone->org_delta = delta;
          
        zone->org_bottom = zone->org_ref;
        zone->org_top    = zone->org_delta + zone->org_ref;
      }
    }
    
    /* sanitize bottom table */
    {
      PSH_Blue_Zone  zone = bot_table->zones;

      for ( count = count_bot-1; count > 0; count--, zone++ )
      {
        FT_Int  delta;
        
        delta = zone[0].org_ref - zone[1].org_ref;
        if ( zone->org_delta < delta )
          zone->org_delta = delta;
          
        zone->org_top    = zone->org_ref;
        zone->org_bottom = zone->org_delta + zone->org_ref;
      }
    }

    /* expand top and bottom tables with blue fuzz */
    {
      FT_Int         dim, top, bot, delta, fuzz;
      PSH_Blue_Zone  zone;

      fuzz  = source->fuzz;      
      zone  = top_table->zones;
      count = count_top;
      
      for ( dim = 1; dim >= 0; dim-- )
      {
        if ( count > 0 )
        {
          /* expand the bottom of the lowest zone normally */
          zone->org_bottom -= fuzz;
          
          /* expand the top and bottom of intermediate zones     */
          /* checking that the interval is smaller than the fuzz */
          top = zone->org_top;
          
          for ( count--; count > 0; count--, zone++ )
          {
            bot   = zone[1].org_bottom;
            delta = bot - top;
            if ( delta < 2*fuzz )
            {
              zone[0].org_top = zone[1].org_bottom = top + delta/2;
            }
            else
            {
              zone[0].org_top    = top + fuzz;
              zone[1].org_bottom = bot - fuzz;
            }
            
            zone++;
            top = zone->org_top;
          }
          
          /* expand the top of the highest zone normally */
          zone->org_top = top + fuzz;
        }
        zone  = bot_table->zones;
        count = count_bot;
      }
    }
  }


 /* reset the blues table */
  static void
  psh_blues_scale_zones( PSH_Blues  blues,
                         FT_Fixed   scale,
                         FT_Pos     delta )
  {
    FT_UInt         count;
    FT_UInt         num;
    PSH_Blue_Table  table = 0;
    
    for ( num = 0; num < 4; num++ )
    {
      PSH_Blue_Zone  zone;
      
      switch (num)
      {
        case 0:  table = &blues->normal_top; break;
        case 1:  table = &blues->normal_bottom; break;
        case 2:  table = &blues->family_top; break;
        default: table = &blues->family_bottom;
      }
      
      zone  = table->zones;
      count = table->count;
      for ( ; count > 0; count--, zone++ )
      {
        zone->cur_top    = FT_MulFix( zone->org_top, scale    ) + delta;
        zone->cur_bottom = FT_MulFix( zone->org_bottom, scale ) + delta;
        zone->cur_ref    = FT_MulFix( zone->org_ref, scale ) + delta;
        zone->cur_delta  = FT_MulFix( zone->org_delta, scale );
        
        /* round scaled reference position */
        zone->cur_ref = ( zone->cur_ref + 32 ) & -64;
      }
    }
    
    /* XXX: we should process the family / normal tables here !! */
  }


  FT_LOCAL_DEF void
  psh_blues_snap_stem( PSH_Blues            blues,
                       FT_Int               stem_top,
                       FT_Int               stem_bot,
                       PSH_Blue_Alignement  alignment )
  {
    PSH_Blue_Table  table;
    FT_UInt         count;
    PSH_Blue_Zone   zone;
    
    alignment->align = 0;
    
    /* lookup stem top in top zones table */
    table = &blues->normal_top;
    count = table->count;
    zone  = table->zones;
    for ( ; count > 0; count-- )
    {
      if ( stem_top < zone->org_bottom )
        break;
        
      if ( stem_top <= zone->org_top )
      {
        alignment->align    |= PSH_BLUE_ALIGN_TOP;
        alignment->align_top = zone->cur_ref;
        break;
      }
    }
    
    /* lookup stem bottom in bottom zones table */
    table = &blues->normal_bottom;
    count = table->count;
    zone  = table->zones;
    for ( ; count > 0; count-- )
    {
      if ( stem_bot < zone->org_bottom )
        break;
        
      if ( stem_bot <= zone->org_top )
      {
        alignment->align    |= PSH_BLUE_ALIGN_BOT;
        alignment->align_bot = zone->cur_ref;
      }
    }
  }

 /*************************************************************************/
 /*************************************************************************/
 /*****                                                               *****/
 /*****                        GLOBAL HINTS                           *****/
 /*****                                                               *****/
 /*************************************************************************/
 /*************************************************************************/

  static void
  psh_globals_destroy( PSH_Globals  globals )
  {
    if (globals)
    {
      FT_Memory  memory;
      
      memory = globals->memory;
      globals->dimension[0].std.count = 0;
      globals->dimension[1].std.count = 0;   
      
      globals->blues.normal_top.count    = 0;
      globals->blues.normal_bottom.count = 0;
      globals->blues.family_top.count    = 0;
      globals->blues.family_bottom.count = 0;
      
      FREE( globals );
    }
  }

  
  static FT_Error
  psh_globals_new( FT_Memory  memory, PSH_Globals  *aglobals )
  {
    PSH_Globals  globals;
    FT_Error     error;
    
    if ( !ALLOC( globals, sizeof(*globals) ) )
      globals->memory = memory;
    
    *aglobals = globals;
    return error;
  }


  static FT_Error
  psh_globals_reset( PSH_Globals  globals,
                     PS_Globals   ps_globals )
  {
    psh_globals_reset_widths( globals, 0, &ps_globals->horizontal );
    psh_globals_reset_widths( globals, 1, &ps_globals->vertical );
    psh_blues_reset_zones( &globals->blues, &ps_globals->blues, 0 );
    psh_blues_reset_zones( &globals->blues, &ps_globals->blues, 1 );
    
    globals->dimension[0].scale_mult  = 0;
    globals->dimension[0].scale_delta = 0;
    globals->dimension[1].scale_mult  = 0;
    globals->dimension[1].scale_delta = 0;
    
    return 0;
  }


  static FT_Error
  psh_globals_set_scale( PSH_Globals    globals,
                         FT_Fixed       x_scale,
                         FT_Fixed       x_delta,
                         FT_Fixed       y_scale,
                         FT_Fixed       y_delta )
  {
    PSH_Dimension  dim    = &globals->dimension[0];
    
    dim = &globals->dimension[0];
    if ( x_scale != dim->scale_mult  ||
         x_delta != dim->scale_delta )
    {
      dim->scale_mult  = x_scale;
      dim->scale_delta = x_delta;
      
      psh_globals_scale_widths( globals, 0 );
    }

    dim = &globals->dimension[1];
    if ( y_scale != dim->scale_mult  ||
         y_delta != dim->scale_delta )
    {
      psh_globals_scale_widths( globals, 1 );
      psh_blues_scale_zones( &globals->blues, y_scale, y_delta );
    }
    
    return 0;
  }


  FT_LOCAL_DEF void
  psh_globals_funcs_init( PSH_Globals_FuncsRec*  funcs )
  {
    funcs->create    = psh_globals_new;
    funcs->reset     = psh_globals_reset;
    funcs->set_scale = psh_globals_set_scale;
    funcs->destroy   = psh_globals_destroy;
  }